1

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???

5
  • 1
    That indeed should not be happening. Is this the entire code? No references used anywhere? More code that changes the value later on? Commented Nov 30, 2012 at 10:45
  • Can't think of a reason that would be happening unless you have only given us a segment of the code... Commented Nov 30, 2012 at 10:46
  • 3
    Please make sure that register_globals isn't enabled on your server. Commented Nov 30, 2012 at 10:47
  • Tested that code on my machine, hit refresh I get Original////Original//// each time. Commented Nov 30, 2012 at 10:48
  • code is working as expected on my machine too... Commented Nov 30, 2012 at 10:50

3 Answers 3

6

Seems like register_globals is enabled on your server. You'll need to disable the directive.

Sign up to request clarification or add additional context in comments.

2 Comments

nice catch,, so long since i have seen a server with that on, you just don't think about it any more.
@Dagon Got caught out by it last year. Host had a "misconfiguration". Suffice to say, I tore my hair out for a while before I figured it out.
1

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.

Comments

-2
<?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

1 Comment

-1, you don't have register_globals enabled that's why it "works for you".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.