I am developing a Php project on LAMP server. Now i face a problem on session variable and local variable.In my systems first.php set the value in session and then in second.php , i set these session variable to my local variable. Then take 开发者_如何学Goaction and clear the Session values.But i also loose the value in my local variable.How can i solve this problem? Structure of coding flow is like as following
In first.php
$_SESSION['var'] = "test";
In second.php
$var = $_SESSION['var'] ;
$_SESSION['var'] = "";
echo $var; // at this time ,i cant also see my local variable values
If that is all you have in your first & second script, then you certainly missed session_start()
in the beginning of either one or all of your script.
Take a good look in your php error log, you might get a better explanation about what's missing and what is wrong. Your code supposed to be running well if you had session_start()
in the beginning of each script that uses session.
If you don't explicitly call session_start()
, then the variable $_SESSION
is nothing more than a user defined variable, so in the first script you assign a value to a user defined variable, and in the second script you merely call to a non existent user defined variable. And be caution that php is case sensitive, $_SESSION
is not the same as $_Session
.
There is no way a variable can be overwritten remotely unless they were assigned by reference, take a look at this example:
$b = 1;
$a =& $b;
echo $a; // will output 1
$b = 2;
echo $a; // will output 2
Here is a nice explanation of how references works in php.
You have register_globals turned on. This is indeed very bad, because it allows injection of variables in your script by a end-user (using cookies, per example).
It's deprecated. Turn it off, quick (in php.ini, or if you can't, in a .htaccess with a php_flag
directive). If your script relies on it, change your script.
精彩评论