Forum Moderators: coopster

Message Too Old, No Replies

timeout script - comparing time

         

jackvull

11:20 am on Jan 25, 2006 (gmt 0)

10+ Year Member



Hi
I am writing a timeout script for my site, which retrieves the last session update from the database and if this is more than say 3 mins, the script redirects the user to login again.

$u = 1 //for testing
$result = mysql_query(" SELECT SessionID,
TIMEDIFF(now(),LastSessionUpdate)
FROM Sessions
WHERECustomerID = '".$u."'");
$res = mysql_fetch_row($result);
$match = $res[0];
$timediff = $res[1];
list($hrs, $mins, $secs) = split('[:]', $timediff);

Now the list function here would give me something like:
000300

I split this into 00 03 00

My worry is that when I compare a value to 03, PHP will not recognise it.

For example: if ($mins >= 3) {header('location:login.php');}

Is there a better way of comparing the time retrieved from a database?

coopster

6:56 pm on Jan 30, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



That could work, unless the days had been two separate days. I think you would probably want to consider using the $hours in that calculation anyway.

If you were using MySQL >= 5.0 you could use TIMESTAMPDIFF:

SELECT 
SessionID,
TIMESTAMPDIFF(MINUTE, LastSessionUpdate, NOW()) AS diff
FROM Sessions ...

The value returned would be the minutes passed.

aaron_d

9:12 pm on Jan 30, 2006 (gmt 0)

10+ Year Member



Wouldn't it be easier just to use the php time() function to get the current and session times? This way you can just us something like:

if($stime > (time()+180))