PHPBuilder - Windows cron job for session



RSS Twitter
Snippets File Management

Windows cron job for session

by: Christof
|
June 4, 2003

Version: 1.0

Type: Sample Code (HOWTO)

Category: File Management

License: GNU General Public License

Description: This cron will do the same on Windows as session management works on Linux



// ************christof's Simple Windows 2k Con Job****************** 
// set the task scheduler in windows 
// C:\PHP\php.exe -q C:\root\to\the\SessionsCron.php
// set you time to always run more frequently than your session lifetime below E.G. Every day, every 15 minutes for 24 hours
// enable the task password with your normal OS admin logins
// make sure the C:\PHP\sessiondata is authorized for R-W or which ever folder you storing your sessions in.
$ThisDir = "C:\PHP\sessiondata";
$ThisFile = "C:\PHP\sessiondata\\";
$SessName = "";

//if you need a database section cleared as well, else comment this out
$MyConn = mysql_connect("host", "user", "pass"); 
mysql_select_db("myDB", $MyConn);

	$handle=opendir($ThisDir); 
	while (false!==($file = readdir($handle))) 
	{ 
		if ($file != "." && $file != "..")
		{
		clearstatcache();
		$ThisFile .= $file;
		$SessName = $file;
		$StatArr = stat($ThisFile);
		$LastAccessedTime = date("H:i:s", $StatArr[9]);
		$NowTime = date("H:i:s");
		$s = strtotime($NowTime) - strtotime($LastAccessedTime);
		$d = intval($s/86400); 
 		$s -= $d*86400;
 		$h = intval($s/3600);
 		$s -= $h*3600;
 		$m = intval($s/60); 
 		$s -= $m*60;
 			if($m >= 20)//lifetime of session in minutes - how long a session may exist for
			{
			unlink($ThisFile);//delete the session file 
			//if you need a database section cleared as well, else comment this out
			$result = mysql_query("DELETE FROM loggedinusers WHERE sessionid = '$SessName'", $MyConn); 
			}
		$ThisFile = "C:\PHP\sessiondata\\"; //reset the root again
		}
	} 
	closedir($handle);
	//if you need a database section cleared as well, else comment this out
	mysql_close($MyConn); 

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Christof

Comment:



Comment:

(Maximum characters: 1200). You have characters left.