How to pass variables between php scripts? How to pass variables between php scripts? php php

How to pass variables between php scripts?


To pass info via GET:

    header('Location: otherScript.php?var1=val1&var2=val2');

Session:

    // first script    session_start();     $_SESSION['varName'] = 'varVal';    header('Location: second_script.php'); // go to other    // second script    session_start();     $myVar = $_SESSION['varName'];

Post: Take a look at this.


Can't you include (or include_once or require) the other script?


You should look into session variables. This involves storing data on the server linked to a particular reference number (the "session id") which is then sent by the browser on each request (generally as a cookie). The server can see that the same user is accessing the page, and it sets the $_SESSION superglobal to reflect this.

For instance:

a.php

session_start(); // must be called before data is sent$_SESSION['error_msg'] = 'Invalid input';// redirect to b.php

b.php

<?phpsession_start();echo $_SESSION['error_msg']; // outputs "Invalid input"