Version: 1.0
Type: Class
Category: Other
License: GNU General Public License
Description: It works like this .
It creates an id for every user that visits the site .
It sets a cookie for him .
The cookie contains the unique id that is a row in a table .
Expired users are automatically remove from db .
$Tracker=new UserTracker();
$Tracker->setVar(“name”,”value”);
$Tracker->getVar(“name”);
$Tracker->delVar(“name”);
//initiate mysql connection before using this . it creates table if missing .
you can keep login count , user status , informations , online users , … , anything .
<?php //You can do anything with this but you can't claim it or remove this line and the next . //Copyright : [email protected] 2004 . send feedback here . class UserTracker { /* $trackerTable; $newSession; $trackerID; $expireTime; $cookieName; isNewSession(); is this the first visit in the seesion. listUsers(); returns an array with all the users available. userCount($max=0); returns and user count . the tricky thing is this : you can set sessions to expire in 1 year . but this doesn't mean the user is online . $max=means the maximum offline time . so you can count the users who loaded your page in the last $max minutes . endSession($uid=""); end this session. get rid of cookie and db info . getIP($uid=""); get the ip of an users . left blank returns yours . getUserAgent($uid=""); get the browser of an users . left blank returns yours . loginTime($uid=""); get the online time of an user . left blank returns yours . this is the time from the tracking begin utill the last time he loaded the page . smaller than expire time . delVar($name,$uid=""); delete a var getVar($name,$uid=""); get a var . this vars can be any php vars . setVar($name,$data,$uid=""); any php var . the content is serialized . lastVisitTime($uid=""); myID(); my uid . getTableName(); table name . setExpiryTime($minutes); session expiry time . this is set when you initiate the class but if you want to give certain users longer sessions you set the time here . getExpiryTime(); get the expire time . */ var $trackerTable="UTracker"; var $newSession=0; var $trackerID=""; var $expireTime=15; var $cookieName="CTracker"; function UserTracker ($table="UTracker",$cookie="CTracker",$minutes=15) { if($minutes<15) $minutes=15; $this->expireTime=$minutes; $this->cookieName=$cookie; $this->trackerTable=$table; $query="CREATE TABLE IF NOT EXISTS `".$this->trackerTable."` ( `UID` int(10) unsigned NOT NULL auto_increment, `ID` varchar(35) NOT NULL, `JTime` int(15) NOT NULL, `Time` int(15) NOT NULL, `Expire` int(10) NOT NULL, `IP` varchar(15) NOT NULL default '', `UserAgent` varchar(255) NOT NULL default '', `Data` text NOT NULL default '', PRIMARY KEY (`UID`) ) TYPE=MyISAM";mysql_query ($query); //remove all the expired sessions . no need to keep them . cookies are long gone anyway . $query="DELETE FROM `".$this->trackerTable."` WHERE (".time()."-Time)>Expire"; mysql_query($query); $this->trackerID=$_COOKIE["$cookie"]; $query="SELECT * FROM `".$this->trackerTable."` WHERE `ID`='".$this->trackerID."'"; $result=mysql_query($query); $rec=array(); $rows=(int)mysql_num_rows($result); if($rows>0) { $rec=mysql_fetch_assoc($result); $this->expireTime=(int)$rec['Expire']/60; } //if the id is not in the database we create a new one even if we have an orphan cookie . if(($this->trackerID=="") || ($rows==0)) { $this->trackerID=md5((string)time().$_SERVER["HTTP_USER_AGENT"].$_SERVER["REMOTE_ADDR"]); //make sure there are no two identical sessions . it can`t happen for two different users from different hosts . $query="DELETE FROM `".$this->trackerTable."` WHERE `ID`=".$this->trackerID; mysql_query($query); setCookie($cookie,$this->trackerID,time()+($this->expireTime*60)); $query="INSERT INTO `".$this->trackerTable."` (`ID`,`Time`,`JTime`,`Expire`,`Data`,`UserAgent`,`IP`) VALUES ('".$this->trackerID."','".time()."','".time()."','".($this->expireTime*60)."','".serialize(array())."','".$_SERVER["HTTP_USER_AGENT"]."','".$_SERVER["REMOTE_ADDR"]."')"; $result=mysql_query($query); $this->newSession=1; } else { setCookie("$cookie",$this->trackerID,time()+($this->expireTime*60)); //make the life of the cookie longer and update time and IP . $query="UPDATE `".$this->trackerTable."` SET `Time`='".time()."',`IP`='".$_SERVER["REMOTE_ADDR"]."' WHERE `ID`='".$this->trackerID."'"; $result=mysql_query($query); $this->newSession=0; } } function getExpiryTime() { return $this->expireTime; } function setExpiryTime($minutes) { if($minutes<15) $minutes=15; $this->expireTime=$minutes; $query="UPDATE `".$this->trackerTable."` SET `Expire`='".($this->expireTime*60)."' WHERE `ID`='".$this->trackerID."'"; $result=mysql_query($query); } function myID() { return $this->trackerID; } function getTableName() { return $this->trackerTable; } function isNewSession() { return (bool)($this->newSession==1); } function setVar($name,$value,$uid="") { if($uid=="") $uid=$this->trackerID; $data=array(); $query="SELECT `Data` FROM `".$this->trackerTable."` WHERE `ID`='".$uid."'"; $result=mysql_query($query); if(mysql_num_rows($result)>0) { $rec=mysql_fetch_array($result); $data=unserialize($rec[0]); if(!is_array($data)) $data=array(); mysql_free_result($result); } $data["$name"]=$value; $query="UPDATE `".$this->trackerTable."` SET `Data`='".serialize($data)."' WHERE `ID`='".$uid."'"; $result=mysql_query($query); } function delVar($name,$uid="") { if($uid=="") $uid=$this->trackerID; $data=array(); $query="SELECT `Data` FROM `".$this->trackerTable."` WHERE `ID`='".$uid."'"; $result=mysql_query($query); if(mysql_num_rows($result)>0) { $rec=mysql_fetch_array($result); $data=unserialize($rec[0]); if(!is_array($data)) $data=array(); mysql_free_result($result); } unset($data["$name"]); $query="UPDATE `".$this->trackerTable."` SET `Data`='".serialize($data)."' WHERE `ID`='".$uid."'"; $result=mysql_query($query); } function getVar($name,$uid="") { if($uid=="") $uid=$this->trackerID; $data=array(); $query="SELECT `Data` FROM `".$this->trackerTable."` WHERE `ID`='".$uid."'"; $result=mysql_query($query); if(mysql_num_rows($result)>0) { $rec=mysql_fetch_array($result); $data=unserialize($rec[0]); if(!is_array($data)) $data=array(); mysql_free_result($result); } return $data["$name"]; } function loginTime($uid="") { if($uid=="") $uid=$this->trackerID; $data=0; $query="SELECT `JTime` FROM `".$this->trackerTable."` WHERE `ID`='".$uid."'"; $result=mysql_query($query); if(mysql_num_rows($result)>0) { $rec=mysql_fetch_array($result); $data=$rec[0]; mysql_free_result($result); } return $data; } function lastVisitTime($uid="") { if($uid=="") $uid=$this->trackerID; $data=0; $query="SELECT `Time` FROM `".$this->trackerTable."` WHERE `ID`='".$uid."'"; $result=mysql_query($query); if(mysql_num_rows($result)>0) { $rec=mysql_fetch_array($result); $data=$rec[0]; mysql_free_result($result); } return $data; } function getUserAgent($uid="") { if($uid=="") $uid=$this->trackerID; $data=""; $query="SELECT `UserAgent` FROM `".$this->trackerTable."` WHERE `ID`='".$uid."'"; $result=mysql_query($query); if(mysql_num_rows($result)>0) { $rec=mysql_fetch_array($result); $data=$rec[0]; mysql_free_result($result); } return $data; } function getIP($uid="") { if($uid=="") $uid=$this->trackerID; $data=""; $query="SELECT `IP` FROM `".$this->trackerTable."` WHERE `ID`='".$uid."'"; $result=mysql_query($query); if(mysql_num_rows($result)>0) { $rec=mysql_fetch_array($result); $data=$rec[0]; mysql_free_result($result); } return $data; } function userCount($max=0) { $query="SELECT COUNT(*) FROM `".$this->trackerTable."`"; if($maxSleep>0) $query.=" WHERE (".time()."-`JTime`)<".($max*60).""; $result=mysql_query($query); $rec=mysql_fetch_array($result); return (int)$rec[0]; } function listUsers() { $uids=array(); $result=mysql_query("SELECT `ID` FROM `".$this->trackerTable."`"); if(mysql_num_rows($result)>0) { while($rec=mysql_fetch_array($result)) { array_push($uids,$rec[0]); } mysql_free_result($result); } return $uids; } function endSession($uid="") { if($uid=="") $uid=$this->trackerID; $query="DELETE FROM `".$this->trackerTable."` WHERE `ID`='".$uid."'"; mysql_query($query); } }; ?>