Who's Online Stats

Discussion in 'PHP' started by FishSword, Sep 4, 2011.

  1. #1
    Hiya,

    What's the best way of finding out and displaying the total number of users currently online, along a breakdown of how many of these users are members or guests?

    I also wish to find out and display the total number of users who were online today, along with a breakdown of how many of these users are members or guests.

    I've noticed that users of the same computer could of course use a different browser preference to another user of the same computer.
    What should happen in this situation?

    Any help is much appreciated.

    Cheers!
     
    FishSword, Sep 4, 2011 IP
  2. ssmm987

    ssmm987 Member

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    43
    #2
    I think, creating a table with user ips, and timestamp from their last visit. (And a boolean column to check if they are members or guests)
     
    ssmm987, Sep 4, 2011 IP
  3. ssmm987

    ssmm987 Member

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    43
    #3
    Something like this:

    
    <?php
    class trackVisitor
    {
    	public $newVisitor;
    	public $id;
    	public $lastvisit;
    	public function __construct($ip,$ismember=0)
    	{
    			$sql= mysql_query("SELECT id,lastvisit FROM  `trackvisitors` WHERE  `ip` LIKE  '".mysql_real_escape_string($_SERVER['REMOTE_ADDR'])."' AND  `ismember` =".(int)$ismember." LIMIT 1")or trigger_error(mysql_error());
    			if(mysql_num_rows($sql)==0)
    			{
    				$this->newVisitor=true;
    				mysql_query("INSERT INTO  `trackvisitors` (
    `id` ,`ip` ,`lastvisit` ,`ismember`)VALUES (NULL ,  '".mysql_real_escape_string($ip)."', CURRENT_TIMESTAMP ,  '".(int)$ismember."');") or trigger_error(mysql_error());
    				$this->id=mysql_insert_id();
    				$this->lastvisit="0000-00-00 00:00:00";
    			} else {
    				$result=mysql_result($sql,0);
    				$this->id=$result['id'];
    				$this->lastvisit=$result['lastvisit'];
    				$this->newVisitor=false;
    				mysql_query("UPDATE  `trackvisitors` SET  `lastvisit` = TIMESTAMP(  '".date("Y-m-d H:i:s")."' ) WHERE `trackvisitors`.`id` =".$result['id']." LIMIT 1") or trigger_error(mysql_error());
    			}
    	}
    }
    class trackVisitorStats
    {
    	public $online;
    	public $members;
    	public $guests;
    	public function __construct($days=7)
    	{
    		$sql=mysql_query("SELECT COUNT(*) 
    FROM  `trackvisitors` WHERE  `lastvisit` > '".date("Y-m-d H:i:s",time()-$days*3600*24)."'")or trigger_error(mysql_error());
    		$this->online=mysql_result($sql,0);
    		$sql=mysql_query("SELECT COUNT(*) 
    FROM  `trackvisitors` WHERE  `lastvisit` > '".date("Y-m-d H:i:s",time()-$days*3600*24)."' AND `ismember` = 1")or trigger_error(mysql_error());
    		$this->members=mysql_result($sql,0);
    		$this->guests=$this->online-$this->members;
    	}
    }
    
    PHP:
    sql:

    
    
    CREATE TABLE IF NOT EXISTS `trackvisitors` (
      
    
    `id` int(255) NOT NULL AUTO_INCREMENT,
      `ip` varchar(255) NOT NULL,
      `lastvisit` timestamp 
    
    NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      `ismember` tinyint(1) NOT 
    
    NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
    
    
    Code (markup):
     
    ssmm987, Sep 4, 2011 IP
  4. FishSword

    FishSword Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #4
    Should multiple users on the website on the same computer be logged as one guest?
     
    FishSword, Sep 4, 2011 IP