Jump to content
Mad_Griffith

PHP passing values from one page to another

Recommended Posts

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 by Mad_Griffith

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 by Mad_Griffith

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 by Mad_Griffith

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...