Page 1 of 1

Time-In and time_out Script using php and mysql

Posted: Fri Sep 10, 2010 2:06 am
by wiljink
Can anyone give me an idea on how to create a time-In and time-Out using php and mysql or can anyone give me an idea.

Re: Time-In and time_out Script using php and mysql

Posted: Fri Sep 10, 2010 6:24 pm
by mecha_godzilla
This code (in PHP) will get you a timestamp:

$_SESSION['timestamp'] = mktime();

Here, the timestamp is being saved as a session value so I can check it later on (to expire a session) but you can assign it to a normal variable. If you want to record it in your DB, create a field that's assigned to the 'timestamp' setting.

If by 'time out' you want a script that tests when a user last did anything on the site (and log them out if their session has expired) you could use this:

Code: Select all

$last_page_action_timestamp = $_SESSION['timestamp'];
$expiry_time = 2*60; // 2 minutes

echo 'Last timestamp was: ' . $last_page_action_timestamp . '<br />';
echo 'Current timestamp is: ' . mktime() . '<br />';
echo 'Expiry time set to ' . ($expiry_time / 60) . ' minutes<br />';
    
if (mktime() > ($last_page_action_timestamp + $expiry_time)) {
    echo 'Timestamp has expired<br />';
 } else {
    echo 'Timestamp is still valid<br />';
}
To see this in action, you need to create the $_SESSION['timestamp'] in one script, then put the second set of code in a different script and keep reloading it to check when the session has expired.

HTH,

Mecha Godzilla