I have a str开发者_StackOverflow社区ange problem in my code that I really can't explain and it just doesn't make any logical sense to me. I am setting a session variable called $_SESSION['user_id']
when a person successfully logs into my website and they are redirected to the index.php
page. This works fine and the session works fine, but then once I refresh the page, the value in the session disappears, but I do not see why.
$long_user_id = $_SESSION['user_id'];
$user_id = substr($long_user_id, 0, -3);
Can anyone see what the problem is here? I thought that the Session variable called user_id
would remain the same, but for some reason it's empty after a second page-load.
Thanks in advance
Edit: I didn't post all my code and I made a mistake by not mentioning that I am using session_start();
on every page. I would also like to confirm that the code works if I comment out the substr
part of the code from the page.
you always have to start and maintain a session first via session_start()
- else your session will disappear. Please note, that you therefore have to call session_start()
on each and every page.
The "solution" to this problem makes very little sense. For some strange reason the $_SESSION['user_id']; was actually being stored as $user_id in the php script. I cannot explain this and I don't know why it was doing it, but what it meant was that when I adjusted the $user_id variable, it was causing problems with the session and thus making it "disappear" as I stated earlier on.
This issue has been resolved by simply changing the variable names to make sure they do not conflict with the session name.
精彩评论