0

I have detected the same thing as another user has already found out - Storing objects in PHP session:

If I store a serialized object (i.e. a string) in $_SESSION['user'] this object disappears with the next page call. It cannot be an problem of mine since all works fine if I use something different as user as key. Is there some magic? Is this reserved?

$_SESSION['user'] becomes false. The key still exists, but the value is false. I'm not quite sure but I think the value gets false after the second call.

Any ideas or hints?

Community
  • 1
  • 1
robsch
  • 9,358
  • 9
  • 63
  • 104
  • Are you sure the variable is not reset in other parts of the code? The fact, that it works with another key, strongly suggests this. – Sirko Sep 24 '12 at 10:07
  • Are you using plain php or do you also use some framework that can possibly overwrite stuff in the session store? – zeebonk Sep 24 '12 at 10:07
  • 2
    Not sure whats wrong but i guess i experiences something similar once. Not sure if it was $_SESSION['user'] but my session variable was also lost. Check if you use variable $user somewhere in your code. If i remembered well in my case was something like this. If you find variable $user somewhere in your code try to change it to something else. I don't know the reason for this, but maybe this is the problem in your case. And also check other things like user above suggested. – StudioArena Sep 24 '12 at 10:12
  • Probably a silly idea but here goes... Have you put session_start() at the top of the page. Maybe you have the error reporting turned down low enough not to see it? – Chris Sep 24 '12 at 10:37
  • @StudioArena: If I've got you right then you are right. I have found out now that if I use for the assignment the variable name for the key name I get the error. Very strange. So $_SESSION['user'] = $user causes the same problem as $_SESSION['user2'] = $user2. $_SESSION['myuser'] = $user works fine. Unfortunately I cannot reproduce it in an example. I don't know why. And yes, I'm quite sure that I didn't forget session_start(). I don't use a framenwork. Nothing special here. – robsch Sep 24 '12 at 11:29
  • 1
    Yeah thats what i want to say. "So $_SESSION['user'] = $user causes the same problem as $_SESSION['user2'] = $user2." But like someone suggested in his answer try to put session_start at the TOP if it's not. Maybe this is a reason. Could be i dont remember where i put this session_start when i experienced this "problem". Hope it works now for you. – StudioArena Sep 24 '12 at 12:41
  • It is already at the top: In globally included file global.php. I think in my case this is not the problem. However, I'm glad to know about this. Is it a bug? Or even a feature ;) ? Would be interesting to know the background too. – robsch Sep 24 '12 at 13:05

1 Answers1

0

The problem may be that the $name is already in the code and/or that session_star() is placed in the wrong place. seesion_start() needs to be always on top of the page!!

faq
  • 2,965
  • 5
  • 27
  • 35