Re: Logging session timeout in DB

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Mar 5, 2008 at 3:08 AM, Angelo Zanetti <angelo@xxxxxxxxxxxxxxx> wrote:
[snip!]
>  No what I would like to know is how can I record when a user closes the
>  browser or the session expires? There is no action for these items. So what
>  would the best way be to keep a record of this? Would the use of a cron job
>  be appropriate? I read in the archives that the banks use javascript but I
>  don't want to go that route.

    One way that comes to mind right off the bat would be to utilize
an idea similar to the following.  You'll need to secure it and tailor
it to your own needs, but it should point you in the right direction.

<?
// Place this function in a file that's included
// in all pages for users that are logged in.
// The field last_seen is INT(10)
function updateLoginTable($uid) {
    $sql = "UPDATE yourTable SET last_seen='".time()."' WHERE
uid='".$uid."' LIMIT 1";
    mysql_query($sql);
}
?>

<?
// This will be your cron job.
// Include your database config here.
// The field is_active is BOOL

$secs = 600; // Number of seconds to wait before considering logged out

$diff_secs = (time() - $secs);

$sql = "UPDATE yourTable SET is_active='0' WHERE last_seen > '".$diff_secs."'";
mysql_query($sql);
?>

    Then, add this to your crontab:
*/1 * * * * `which php` /path/to/cron/script.php

    It will run every minute, making users who have not accessed a
page within the previous 10 minutes inactive.  It's untested, but
hopefully it will point you in the right direction anyway.

-- 
</Dan>

Daniel P. Brown
Senior Unix Geek
<? while(1) { $me = $mind--; sleep(86400); } ?>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux