Gossamer Forum
Home : General : Perl Programming :

User Sessions

Quote Reply
User Sessions
I'm writing a script that uses a sessions table. I have written the code for a who's online routine that only selects users that are still within their 2 hour session. Also I have written the code so that rows in the session table are deleted if the timestamp is less than the currect time less the two hour session or if someone logs out.

The problem I'm having is that what happens if someone doesn't logout when they leave? ....the row wont be removed and they'll show online until their two hour session expires.

How can I combat this?

Also would it be best two have a 2 hour session then end it or should I continually update the timestamp if they remain active so the two hours only starts when they aren't active?....oh and how should I combine it with cookies?

Thanks.

Last edited by:

PaulW: Dec 6, 2001, 9:26 AM
Quote Reply
Re: [PaulW] User Sessions In reply to
G'Day Paul,

I just finished working with my session code. I made the time frame 15 minutes of inactivity before they are logged out. What I did in my code was on each page they visit, the "action" field is updated as to where on the site they are. The code to do this has a small section in it...

Code:

mt_srand ((double) microtime() * 1000000);
if (mt_rand(0, 20) < 4){
$time = time(); $session_life = $CFG['session_life'];
$expire_time = $time - $session_life;
$query = "delete from ${DB_CFG['Sessions']} where lastAction < '$expire_time'";
$result = $DB->query($query);
}

Which just checks on 20% of all events wether or not to delete old sessions. time() in PHP returns the ammount of seconds since 1970, can't remember if its the same in PERL or not.

I generate a random 32 digit number as the Session_ID and attach it to it to every single URL and form. Session_ID is the primary key in the sessions table.

This way if they aren't logged out, someone else visiting the site runs the query to delete old sessions as well so they'll be logged out. Obviously you would change the 20% based on how much traffic you have. If you get ALOT you would make it 1% or 2% - hardly any you bump it up to 100%.

Hope that helps.
Cheers,
Michael Bray
Quote Reply
Re: [Michael_Bray] User Sessions In reply to
In Reply To:
time() in PHP returns the ammount of seconds since 1970, can't remember if its the same in PERL or not.
yep, same in both Perl and PHP - the "epoch".


Adrian