PDA

View Full Version : $_SESSION problem



imagined
September 20th, 2007, 12:00 PM
I used to name the $_SESSION variable $_SESSION['user']. I was logged in with my webhosting provider. I tried to echo the session variable and my username of my webhosting provider would echo instead of the username of the script i was testing!

So I changed the $_SESSION variable name to $_SESSION['id']. I have a login script that records the user's id with the session variable $_SESSION['id']

But sometimes, when I try to echo it on a form, it comes out as Array.

So to find out what was going on I checked what was inside that array by doing this:


foreach($_SESSION as $printit)
{
echo '<li>'.$printit.'</li>';
}


It print two values:
1
Array

It was supposed to print the number 18 instead of the word array.


Any idea of what this problem might be?

MTsoul
September 20th, 2007, 04:41 PM
http://us2.php.net/print_r

Anthony:-P
September 22nd, 2007, 10:28 PM
Hi imagined,

print_r($_SESSION); will show all of the current sessions you have stored. That should output the value you require. If it comes out as a big array use:

echo "<pre>";
print_r($_SESSION);
echo "</pre>";

which will display it in a more..user friendly way.

If you still can't find out why,then caan you paste the code you used to set the variable please?

Kind Regards,
Anthony:-P

imagined
September 24th, 2007, 12:08 PM
Thanks guys!

Yes, I found the problem. I feel kinda stupid hehe. I had this problem long time before.

Looks like PHP sees $_SESSION['id'] the same as $id.

And the other problem was that PHP would print out the session of another page I had on my browser instead of the session on the script I was on. I have no idea why this was happening. I just had to change the name of the $_SESSION variable from $_SESSION['user'] to $_SESSION['userid'].