Zend - The PHP Company




Authentication

Add Code


Ultimate user tracker with PHP , mySql and COOKIES .  

Type: class
Added by: darkestlight
Entered: 03/04/2004
Last modified: 04/12/2003
Rating: - (fewer than 3 votes)
Views: 7771
Easy to use PHP & mySql user tracker .
It combines a cookie with an id with a database to store informations . The cookie will hold an ID and then the vars and everything is read and written to the database . Perfect for user tracking and login systems . Rate it . I will share more code if appreciated properly .


<?php
//You are free to do anything with this as long as you don't claim it or remove this and the next line .
//Copyright : darklight@go.ro .
class UserTracker
{
/*
    $trackerTable;
    $newSession;
    $trackerID;
    $expireTime;
    $cookieName;

    isNewSession();
    listUsers();
    userCount($max=0);
    endSession($uid="");
    getIP($uid="");
    getUserAgent($uid="");
    loginTime($uid="");
    delVar($name,$uid="");
    getVar($name,$uid="");
    setVar($name,$data,$uid="");
    lastVisitTime($uid="");
    myID();
    getTableName();
    setExpiryTime($minutes);
    getExpiryTime();
*/
    
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);
    }
};
?>


Usage Example


$Tracker=new UserTracker("SUserTracker","surveyorTracker",24*7*60);//1 week

$Tracker->setVar("IsAdmin",0);
echo $Tracker->getVar("IsAdmin");
//make sure you create the tracker before any output


Rate This Script





Search



This Category All Categories