Removing obsolete session from the DB

Abstract

Somewhere you may have a table with active_session of visitors using your site. From time to time you would like to remove obsolete session. The script below is using mktime() to fit the timestamp stored in the DB. I use to remove session in this example that are older than two days.

Script

<?php
/**
* Author: Urs Gehrig <urs_at_circle_ch>
* Date: 27-3-2001
*
* Subject: how you could remove your sessions older
* than _x_ days from your database.
*/
require_once(\”prepend.php\”);
page_open(array( \”sess\” => \”ac_session\”,
\”auth\” => \”ac_default_auth\”,
\”perm\” => \”ac_perm\”,
\”user\” => \”ac_user\” ));
?>
<?php
/**
* delete sessions from database older than 2 days
* delete ac_user and ac_session
*/
$db = &new db_yourdb();
$cfg->active_sessions = \”ac_active_sessions\”;

/**
* building the reference date
* $expired contains something like \”20010327231519\”
*/
$expired = strftime(\”%Y%m%d%H%M%S\”,
mktime(
date(\”H\”),
date(\”i\”),
date(\”s\”),
date(\”m\”),
date(\”d\”)-2,
date(\”Y\”)
) );
/**
* building the sql query
*/
$sql = \”DELETE FROM
$cfg->active_sessions
WHERE
changed < \’$expired\’
AND
name
LIKE
\’%ac_%\’\”;

/**
* executing the query
*/
$db->query($sql);

?>
<?php
page_close();
?>

to be cont\’d

Links

http://phplib.netuse.de – PHPLIB home
http://ch.php.net/mktime – PHP function reference

Leave a Reply