Somk
Somk

Reputation: 12047

PHP Session variable defined by wrong variable

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

Upvotes: 1

Views: 54

Answers (2)

Marc B
Marc B

Reputation: 360682

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.

Upvotes: 2

John Conde
John Conde

Reputation: 219814

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.

Upvotes: 2

Related Questions