Location: PHPKode > scripts > PHP Online Users > php-online-users/online.class.php
<?php
class whoIsOnline {
	
	var $time_to_remain_online;
	var $max_time_online;
	
	function whoIsOnline() { }
	
	function setSessionTime($mins) {
		$this->time_to_remain_online = time() + ($mins * 60);
		$this->max_time_online = $mins * 60; //cleanup time
	}
	function isOnline($user_id) {
		$check = false;
		$a = mysql_query("SELECT user_name FROM `online` WHERE user_id=$user_id LIMIT 0,1");
		if($a) {
			if(mysql_num_rows($a) > 0) {
				$check = true;
			}
			mysql_free_result($a);
		}
		return $check;
	}
	function recordUser() {
		global $CURUSER;
		//check if user is not guest
		if(array_key_exists('user_id',$CURUSER)) {
			//first time visit?
			$tco = $this->time_to_remain_online;
			if(!isset($_SESSION['user_online']) || ($_SESSION['user_online'] - time()) <=0) {
				if($this->isOnline($CURUSER['user_id'])) {
					mysql_query("UPDATE `online` SET log_time=$tco WHERE user_id=$CURUSER[user_id]");
				}
				else {
					mysql_query("INSERT INTO `online` (`user_id`, `user_name`, `log_time`) VALUES($CURUSER[user_id],'$CURUSER[user]',$tco)");
				}
				$_SESSION['user_online'] = $tco;
			}
		}
	}
	function removeOnlineUser($user_id) {
		mysql_query("DELETE FROM `online` WHERE user_id=$user_id");
		if(isset($_SESSION['user_online'])) {
			unset($_SESSION['user_online']);
		}
	}
	//peform cleanup after certain period of time ... this function will be called by cron
	function cleanup() {
		$inactive = time()-$this->max_time_online;
		
		$query = "DELETE FROM `online` WHERE `log_time` <= $inactive" ;
		mysql_query($query);
	}
}
?>
Return current item: PHP Online Users