I'm writing a PHP login script after reading a lot about session security online. However, I'm having a problem where the session information is not consistent between pages. There is probably a behavior involved with one of the methods I'm using that I've overlooked, but I'm not exactly a PHP or session expert. The code where I'm having problems is in the session starting function that is called in the head
of every page:
//Setup the session
session_name('CPI_SESSION');
session_set_cookie_params(0, '/', $_SERVER['SERVER_NAME'], (isset($_SERVER['HTTPS']) ? $_SERVER['HTTPS'] : true), true);
//Start the session
session_start();
print_r($_SESSION);
echo '<br/>';
if(!isset($_SESSION['session_init']))
{
session_regenerate_id(true);
$_SESSION['session_init'] = true;
print_r($_SESSION);
}
The method I'm using I read about here. The only thing I modified is for session_regenerate_id
to delete the old session, which is a behavior I want. However, I get unexpected results. The echo
I get looks like this:
Array ( )
Array ( [session_init] => 1 )
From each print_r
respectively. So each time the function is called, for some reason beyond my knowledge the session is empty, which causes it to regenerate the id and clear the already empty session, then it sets session_init
. If I refresh the page, session_init
should still be saved, but it's not. This causes it to do the same thing over again. Why is this? I've tried commenting out session_name
and session_set_cookie_params
but it didn't make a difference.
I'm running this locally with an Apache 2.2 server and PHP 5.3.