Jump to content

PHP passing values from one page to another


Mad_Griffith

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
Link to comment
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
Link to comment
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.

Link to comment
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
Link to comment
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.

Link to comment
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.

Link to comment
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.

Link to comment
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.

Link to comment
Share on other sites

  • 2 weeks later...

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...