Jas01724
09-09-2005, 05:13 AM
Instead of the default flat files, I'm using a database to store user sessions because of the extra control. Keeping the session alive in the database is easy, all I have to do is assign a value of "1" to the row's "keep_alive" column ... however that doesn't solve the problem of kick-starting a user's session again if their PHPSESSID cookie has expired.
So far I've got the following code to work, and it involves setting a separate "keep_alive" cookie in addition to the standard session cookie:
// Only do this if the session cookie is unset and the "keep alive"
// cookie is set; "keep alive" contains the user's previous session id
// in a sha1() hash
if(!isset($_SESSION['session_name']) && isset($_COOKIE['keep_alive']))
{
// open database class
$session = new db('','','','');
// pull session id from all sessions where "keep_alive" is set
$session = $session->rows('SELECT session_id FROM session_table WHERE keep_alive = "1"');
// if $session contains database row(s)
if($session != '')
{
// Do any session id's match the "keep alive" cookie
$session_match = 0;
// Check each session_id for a match with the "keep alive" cookie
foreach($session as $a)
{
// if sha1() hash of session_id is the same as value in "keep alive"
// cookie, set new PHPSESSID containing user's previous session id,
// set $session_match to "1", break out of foreach, reload requesting
// page with reset session
if(sha1($a->session_id) == $_COOKIE['keep_alive'])
{
setcookie('PHPSESSID',$a->session_id,0,'/');
$session_match = 1;
header('Location: ' . $request);
break;
}
}
}
// if no rows were pulled from database, or if none of the database rows
// matched the value of the "keep alive" cookie and $session_match wasn't
// set, kill redundant cookie
if($session == '' || $session_match == 0)
{
setcookie('keep_alive',0,time()-5,'/');
}
}
Does anyone foresee any potential problems with this method? It does work, but I'm not sure how secure or "foolproof" it is ... not to mention there may be an all around better way to it. All session names and cookie names will contain a hash of the user's user agent to help prevent hijacking by another user.
So far I've got the following code to work, and it involves setting a separate "keep_alive" cookie in addition to the standard session cookie:
// Only do this if the session cookie is unset and the "keep alive"
// cookie is set; "keep alive" contains the user's previous session id
// in a sha1() hash
if(!isset($_SESSION['session_name']) && isset($_COOKIE['keep_alive']))
{
// open database class
$session = new db('','','','');
// pull session id from all sessions where "keep_alive" is set
$session = $session->rows('SELECT session_id FROM session_table WHERE keep_alive = "1"');
// if $session contains database row(s)
if($session != '')
{
// Do any session id's match the "keep alive" cookie
$session_match = 0;
// Check each session_id for a match with the "keep alive" cookie
foreach($session as $a)
{
// if sha1() hash of session_id is the same as value in "keep alive"
// cookie, set new PHPSESSID containing user's previous session id,
// set $session_match to "1", break out of foreach, reload requesting
// page with reset session
if(sha1($a->session_id) == $_COOKIE['keep_alive'])
{
setcookie('PHPSESSID',$a->session_id,0,'/');
$session_match = 1;
header('Location: ' . $request);
break;
}
}
}
// if no rows were pulled from database, or if none of the database rows
// matched the value of the "keep alive" cookie and $session_match wasn't
// set, kill redundant cookie
if($session == '' || $session_match == 0)
{
setcookie('keep_alive',0,time()-5,'/');
}
}
Does anyone foresee any potential problems with this method? It does work, but I'm not sure how secure or "foolproof" it is ... not to mention there may be an all around better way to it. All session names and cookie names will contain a hash of the user's user agent to help prevent hijacking by another user.