Is there any way to pass values and variables between php scripts?
Formally, I tried to code a login page and when user enter wrong input first another script will check the input and if it is wrong, site returns to the last script page and show a warning like "It is wrong input". For this aim, I need to pass values from scripts I guess.
Regards... :P
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
<?php
session_start();
echo $_SESSION['error_msg']; // outputs "Invalid input"
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With