Have to login twice. PHP sessions and login troubles with Chrome and Opera.

Posted by Robert on Stack Overflow See other posts from Stack Overflow or by Robert
Published on 2010-03-08T23:40:12Z Indexed on 2010/03/08 23:51 UTC
Read the original article Hit count: 312

Filed under:
|
|
|

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.

© Stack Overflow or respective owner

Related posts about php

Related posts about session