Mad_Griffith Posted June 9, 2016 Share Posted June 9, 2016 (edited) Hi, I have the following problem in PHP and I wouldn't know how to do it... could you help me out? Thanks! page 1 if(x is not set) { go to page 2 } else { access variable x's content and do something with it } page 2 x = something go back to page 1 Edited June 9, 2016 by Mad_Griffith Link to comment Share on other sites More sharing options...
Ingolme Posted June 9, 2016 Share Posted June 9, 2016 You can transfer values across pages using the session: http://www.w3schools.com/php/php_sessions.asp One thing they didn't mention in the tutorial is that you should call session_write_close() before redirecting to store the session values. Link to comment Share on other sites More sharing options...
Mad_Griffith Posted June 9, 2016 Author Share Posted June 9, 2016 (edited) Ok, thanks! I have the additional problem that the data is coming from an AJAX request and there's Twitter involved. This is what I am currently doing (and it is not working): my page 1 session_start() if(whatever) { $_SESSION['myData'] = $_POST['MyData']; session_write_close() echo $url; // I get this response in AJAX and I open it in a popup back through the "then" method of a JS promise } else { // Do something with $_SESSION['myData'] } twitter auth page once logged in and accepted the app, goes to page 2 my page 2 session_start() // some other stuff happens, but $_SESSION['myData'] does need to be manipulated on this page session_unset();session_destroy(); header( go back to page 1, evaluates the else block) Edited June 9, 2016 by Mad_Griffith Link to comment Share on other sites More sharing options...
Ingolme Posted June 9, 2016 Share Posted June 9, 2016 Don't call session_unset() or session_destroy() on any page or all your data will be lost. session_write_close() is only needed if you're using PHP's header() function for redirecting. If it's Javascript doing the redirect then there's no need for it. Link to comment Share on other sites More sharing options...
Mad_Griffith Posted June 9, 2016 Author Share Posted June 9, 2016 (edited) Ok thanks, so I moved them at the end of the else block. This is what I have so far and it works only if I unset the variables/destroy the session every time. my page 1 session_start() if(whatever) { $_SESSION['myData'] = $_POST['MyData']; session_write_close() echo $url; // I get this response in AJAX and I open it in a popup back through the "then" method of a JS promise } else { // Do something with $_SESSION['myData'] session_unset();session_destroy(); } twitter auth page once logged in and accepted the app, goes to page 2 my page 2 session_start() // some other stuff happens, but $_SESSION['myData'] does need to be manipulated on this page header( go back to page 1, evaluates the else block) Edited June 9, 2016 by Mad_Griffith Link to comment Share on other sites More sharing options...
justsomeguy Posted June 9, 2016 Share Posted June 9, 2016 Why are you unsetting the session variables at all? Why does it break if you have things in the session? That's specifically what the session is for - keeping data across page accesses. Also, please use a code box when you're posting code, so that it is easier to read. There's a button in the text editor to add a code box. Link to comment Share on other sites More sharing options...
Mad_Griffith Posted June 9, 2016 Author Share Posted June 9, 2016 As far as I understand, initiating a session allows me to pass variables coming from an AJAX request easily between files. Link to comment Share on other sites More sharing options...
justsomeguy Posted June 9, 2016 Share Posted June 9, 2016 The source of the data isn't important, you can store data from anywhere in the session. Note that the session is only available in PHP, you cannot directly access the PHP session from Javascript, for example. But, yes, when you store data in the session it is available on any other PHP page where that session is active. The only time you would ever really need to unset session variables or destroy the session is when the user logs out. Link to comment Share on other sites More sharing options...
Mad_Griffith Posted June 10, 2016 Author Share Posted June 10, 2016 (edited) This I understand. But I am having lots of problems with Twitter's auth popup so I decided to kill the session everytime... Edited June 10, 2016 by Mad_Griffith Link to comment Share on other sites More sharing options...
Ingolme Posted June 10, 2016 Share Posted June 10, 2016 The session has nothing to do with whatever other issues you're encountering. If you're going to delete the session data every time then you're not any better off than having no session at all. Link to comment Share on other sites More sharing options...
Mad_Griffith Posted June 10, 2016 Author Share Posted June 10, 2016 I agree. But I don't manage to proceed any further because I am inexperienced with PHP and AJAX requests and I wouldn't know how to build or debug this... Link to comment Share on other sites More sharing options...
justsomeguy Posted June 10, 2016 Share Posted June 10, 2016 The session wouldn't contain any data from Twitter, the session is only for your PHP pages to access data that you set. Twitter does not have access to your PHP session, and vice versa. Twitter has their own session. Link to comment Share on other sites More sharing options...
sandeepm Posted June 24, 2016 Share Posted June 24, 2016 By using GET/ POST method you can transfer the data from one page to another page. Link to comment Share on other sites More sharing options...
Ingolme Posted June 24, 2016 Share Posted June 24, 2016 By using GET/ POST method you can transfer the data from one page to another page. GET and POST on their own will not allow data to persist between pages, they are just used to send data from the client to the server. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now