views:

151

answers:

2

Hello,

I'm working in a RIA. We use Memcached to store sessions, and I've installed http://pecl.php.net/package/memcache and my PHP session handler looks like this:

$session_save_path = "tcp://$host:$port?persistent=1&weight=2&timeout=2&retry_interval=10,  ,tcp://$host:$port  ";
ini_set('session.save_handler', 'memcache');
ini_set('session.save_path', $session_save_path);

The session timeout is set to 30min. In my RIA I want periodicly call a serverside script via AJAX to check if the visitor's session is still alive. If the ajax calls returns false I blackout the screen and show a pretty relogbox to continue the session.

Now the problem is with the serverside script. I need to determine if the session exists without extending the lifetime of the session if it does exists.

I'm not completely knowladble about the workings of the session handler, but i'm pretty sure if i would do this:

<?
session_start();
if($_SESSION['loggedin'] == "yes")
    echo "true";
else 
    echo "false";
?>

I'm pretty sure this would renew the session's lifetime (on the serverside, but also on the clientside by sending a new cookie back to the client). And the session would exist indefinetly.

Some options i considered, but excluded:

  • Don't do any serverside calls, but use a javascript timer on the client (expires after 30min for example). This won't work when the user has the RIA open in multiple windows
  • Try to hack around the session_start() to prevent it from sending a new fresh cookie back to the client. This might work for the clientside, but the expirationtime would still be refreshed at the internal session_handling.

I'd like some idea's, T.i.a.

A: 

Don't validate your users with Javascript alone. You're just asking for some serious security issues.

Your first method of checking with Ajax for $_SESSION['loggedin'] will work - if their previous session has expired, it will generate a new session id before continuing and so $_SESSION['loggedin'] will not be set.

Jarrod
This isn't validation, I validate users each page reload on the serverside. I want this purely for convenience of the user.With the first method, if my user logs in at 14:00, and does nothing, his sesison will expire at 14:30, but the javascript in the background calls taht script via AJAX each 5min, won't his session time be renewed every call (e.g. 14:35, 14:40, 14:45, 14:50). Meaning his session will _never_ timeout. That's the issue here, the session_start causing the session's lifetime to be renewed.
Kwaak
+1  A: 

You don't have to equate the session timeout with the authorization timeout. I would suggest storing an extra variable in the session, a timestamp of when the user logged in. Then you can consider that the user logged out if the session doesn't exist or the timestamp is too old. As a side effect it will also give you extra precision because the session is not guaranteed to expire exactly when you've set it, but may linger around for a while longer until the garbage collection runs.

As a matter of fact I'd suggest you wrap this functionality in a simple class and do something like this:

$acl->logIn($username); //set the user as logged in
$acl->isLoggedIn($username); //Is he still logged in?

etc, etc

Manos Dilaverakis
Yeah, that would mean introducing an entire new timeout variable into my session management and auth management just for this. I'd rather avoid that if possible.
Kwaak
@Kwaak - Unless you've been copy-pasting authorization code all over the place the most minimalistic version of this can be implemented in 2-3 lines of code. I don't know if you'll be able to find a solution that requires less work to implement.
Manos Dilaverakis