Reputation: 13
I'm experiencing a strange PHP session issue. Can someone tell me if that's how session works?
To see the problem, load the following code into any php file, say test.php, and run it 2 times. NOTE, you have to run it two times, i.e. load the page and reload it.
<?
session_start();
$_SESSION["test"] = "Original////";
$test=$_SESSION["test"];
echo $_SESSION["test"];
$test="New////";
echo $_SESSION["test"];
?>
On my server, the first time I load this test page, I get
Original////Original////
and that is correct. But when I reload it, I get
Original////New////
which means the 5th line "$test="New////";" actually rewrite my $_SESSION["test"]. That doesn't make sense to me. Anyone knows what is happening? Or it's just happening on my server???
Upvotes: 1
Views: 130
Reputation: 1
<?php
session_start();
$_SESSION["test"] = "Original////";
$test=$_SESSION["test"];
echo $_SESSION["test"];
$test="New////";
echo $_SESSION["test"];
?>
I have tried your code in my environment its running perfectly.its always print Original////Original//// so its happening only on your server
Upvotes: -2
Reputation: 10467
Firstly, do not use <?
as a starting tag of PHP, please use <?php
. Secondly, this is an expected behavior if you have register_globals
enabled. Look at this link:
http://www.theblog.ca/session-register-globals
It's title says:
When register_globals is on, session variables overwrite global variables
And sample code is similar to yours:
<?php
session_start();
$canadaday = 'July 1st';
$_SESSION['canadaday'] = 'July 2nd';
print '<p>When is Canada Day?</p>';
print '<p><strong>' . $canadaday . '</strong></p>';
?>
With register_globals
, result is July 2nd
. HTH.
Upvotes: 1
Reputation: 19879
Seems like register_globals is enabled on your server. You'll need to disable the directive.
Upvotes: 6