0

I have encountered a weird situation:

First I assign variable to a session then I change it. Then the session changes when I change the variable, even when I didn't re-assign the value to the session :

the example (session1.php)

session_start();
$var_test = "first session value";
$_SESSION['var_test'] = $var_test;
echo "$_SESSION['var_test'];

// this will show "first session value"

the second php file (session2.php)

session_start();
$var_test = "second session value !!!";
echo $_SESSION['var_test'];

// this shows "second session value !!!";

In session1.php it shows "first session value" which is perfectly good, but in the second php file (session2.php) it shows "second session value".

Even though I didn't assign $_SESSION['var_test'] = $var_test, how come does it change automatically?

Is that a bug or is it just how it works?

2
  • cannot reproduce your case. Your code works fine here. Commented Oct 1, 2014 at 12:59
  • And what happens if you don't change the variable? Just curious. Commented Oct 1, 2014 at 12:59

1 Answer 1

1

This could probably be caused by register_globals=on in php ini. (If you are using an old php version)

try this in your .htaccess file (located in document root):

php_flag register_globals off. 

(Our if you have access: change it in php.ini);

register_globals is a security threat for a bad written php application.

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

5 Comments

I think you are right, my register_globals is currently set as "ON" and I'm using php 5.2.6
Im bit scared if I change it to "OFF" it might crush my entire site as I dont know how will it affect all my pages ... :(
You should realy consider to turn it off! But when you do: test your applications since there could be some side effects for your curent scripts if they are poorly written.
You could always turn it back on!
Glad I could be of help!

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.