Have to login twice. PHP sessions and login troubles with Chrome and Opera
The problem I am encountering is that for my login form I have to login twice for the session to register properly, but only in Chrome (my version is 4.0.249.89) and Opera (my version is 10.10).
Here is the stripped down code that I am testing on:
Login Page:
session_start();
$_SESSION['user_id'] = 8;
$_SESSION['user_name'开发者_开发技巧] = 'Jim';
session_write_close();
header('Location: http://www.my-domain-name.com/');
exit();
Home Page:
session_start();
if ( isset($_SESSION['user_id']) )
{
echo "You are logged in!";
}
else
{
echo "You are NOT logged in!";
}
Logout Page:
session_start();
session_unset();
session_destroy();
header('Location: http://www.my-domain-name.com/');
exit();
Currently, under a fresh load with no cookies, if I go to my-domain-name.com/login/ it will redirect to the home page and say "You are NOT logged in!" but if I go there again it will say "You are logged in!". Any ideas?
Thanks for your help.
Try adding a sleep(2);
before issuing the redirect header; that'll tell you whether it's actually a bug in your code somewhere, or if the session data just isn't being written to file fast enough.
I got the same problem like this. I Just simply deleted the www. in the header section.
Then it worked fine for me. It will be like this: header('Location: http://my-domain-name.com/');
Your code, as presented, seems that it would cause an infinite redirect loop with the home page calling session_start(), setting the cookie, and instructing the browser to load the home page. Is there some logic missing from the code presented here?
精彩评论