Forum Moderators: coopster

Message Too Old, No Replies

sessions failing randomly

site's sessions failing randomly only on hosting env not localhost

         

sunswept

8:21 pm on Jul 25, 2007 (gmt 0)

10+ Year Member



My site's sessions are failing for apparently no reason.
The code works on my local machine perfectly but in the hosting env it doesnt for long periods of time.

My session settings are as follows (they are placed in a config.php file that is included in scripts)
ini_set( 'session.use_cookies', '1' );
ini_set( 'session.use_only_cookies', '1' );
ini_set( 'session.cookie_lifetime', '0' );
ini_set( 'session.gc_maxlifetime', '7200' );
ini_set( 'session.save_path', SESSIONS_DIR );
(permission for the session_dir are 777)

To handle sessions i have the following functions


function createSession ( )
{
session_start();
if ( isset($_SESSION[ 'auth' ]) ) {
//Session already exists, not created
return false;
}
$_SESSION[ 'auth' ] = 'done';
return true ;
}
function verifySession( )
{
session_start();
if( isset($_SESSION['auth']) && ($_SESSION['auth'] == 'done') ){
return true ;
}
else{
// delete the session created for verification
$_SESSION = array();
if( isset($_COOKIE[session_name()]) ){
setcookie(session_name(),'',time()-42000, '/');
}
session_destroy();
return false ;
}
}

To use the session i call the createSession(). In subsequent scripts I call verifySession() to check sesion validity.

The problem is that the verifySession function keeps returning false in my hosting env. This happens continuously for long periods of time and then stops happening (apparently all by itself).
I observed that the hosting server changed time (moved back) in one such long duration.
I am absolutely clueless about why this could be happening. Any thoughts about what could be causing this? Any suggestions about additions to the session handling code will be appreciated.

Thanks

barns101

12:52 am on Jul 26, 2007 (gmt 0)

10+ Year Member



Is SESSIONS_DIR in your webspace or your host server's temp directory?

On shared servers garbage collection from other users running short-life sessions can have the effect of prematurely killing your sessions. The resolution is to use a directory within your webspace to store your sessions date instead.

sunswept

4:49 am on Jul 26, 2007 (gmt 0)

10+ Year Member



barns the dir is in my own webspace. I have checked and the sessions are actually created in it, but for some reason they are not read by the verifySession function.

WesleyC

2:09 pm on Jul 26, 2007 (gmt 0)

10+ Year Member



You could create your own session handler in PHP to use SQL for session storage if it's available. This is far faster than writing files, and if well-written, should resolve the issue.

There's a couple good, simple scripts publicly available through a Google search, or you can write your own using session_set_save_handler (look up in the PHP documentation).