1
<?php
session_start(); 
$id = 'dd';
print_r($_SESSION);
?>

When I go to this page the variable $_SESSION['id'] is defined as a number. But on this page I define the variable $id and then when I print the $_SESSION it has changed the variable $_SESSION['id'] to 'dd'. How is this possible?

0

2 Answers 2

2

There's only two ways for your code to work as stated:

1) You've got register_globals on, and your session already had a id parameter set

That means you're on an OLD php install, and/or a horribly badly configured one. Register_globals is pretty much the single greatest STUPIDITY in the history of PHP, and it has thankfully been eliminated from "modern" versions of PHP.

2) You created a reference beforehand, e.g.

$_SESSION['id'] = 'foo';
$id =& $_SESSION['id']; // $id now points at the session variable
echo $id; // prints foo
$id = 'bar'; // also changes the session value, because of the referencing.
echo $_SESSION['id']; // prints 'bar', because of the referencing.
Sign up to request clarification or add additional context in comments.

Comments

2

You have register globals turned on. This causes the declaration of $id to overwrite the $_SESSION['id'] since they point to the same place.

You should turn this off as it is deprecated and can cause issues like you are experiencing.

Comments

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.