php script using that i can check how many visitors are browsing my site

Discussion in 'PHP' started by unna, Mar 29, 2010.

  1. #1
    hi friends...

    i want to have a php script using that i can check how many visitors are browsing my site?

    like DP Currently Active Users...

    help me if anybody can...

    thanks
     
    unna, Mar 29, 2010 IP
  2. fierceservers

    fierceservers Peon

    Messages:
    338
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    fierceservers, Mar 29, 2010 IP
  3. Narrator

    Narrator Active Member

    Messages:
    392
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    80
    #3
    It is really easy to do.
    First you need a table in your database for visitors. You will need fields for ip addresses and timestamps.

    
    function logVisitor(){
    	$wait=300;   // how long before the user is offline, in seconds
    	$offline=time()-$wait;
    	$sql="DELETE FROM visitors WHERE timestamp<'$offline'"; //Delete the rows for offline visitors
    	$query=mysql_query($sql);
    	$ip=$_SERVER['REMOTE_ADDR'];
    	$lastAction=time();
    	$sql="SELECT * FROM visitors WHERE ip='ip'";
    	$query=mysql_query($sql);
    	$ipCheck=mysql_num_rows($query);
    	if($ipCheck==0){
    		$sql="INSERT INTO visitors (ip, timestamp) VALUES ('$ip', '$lastAction')";
    		$query=mysql_query($sql);
    	}else{
    		$sql="UPDATE visitors SET timestamp='$lastAction' WHERE ip='$ip'";
    		$query=mysql_query($sql);
    	}// end ipCheck
    }//end logVisitor function
    
    function numOnline(){
    	$sql="SELECT * FROM visitors";
    	$query=mysql_query($sql);
    	$numOnline=mysql_num_rows($query);
    	return $numOnline;
    }//end numOnline function
    
    PHP:
    Basically it logs each visitor's last action, to determine how many are online. Edit $wait to however long you think it should be.

    You will run the function logVisitor() on each page. And numOnline() when you want to know how many is online.

    The same can be used to determine what users are online if you have a membership script. Simply add a lastAction field to the members table to determine who is online.
     
    Narrator, Mar 29, 2010 IP
  4. K.Meier

    K.Meier Well-Known Member

    Messages:
    281
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #4
    That is a nice method, but if you have a bigger website with several 1000 users online at the same time and for each click they make, you update your MySQL Server on multiple rows, your Server will go down sooner or later. But for a basic normal website its very nice script.
     
    K.Meier, Mar 29, 2010 IP
  5. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #5
    You could try something like
    
    $activeUsers = count(glob(session_save_path().'/sess*'));
    
    PHP:
    Works only if session.save_path is set in php.ini, defaults to '/tmp'.
    Of course, it has it's downsides, but it should work 99% of the time. Read about PHP sessions if you are unsure how it works.
     
    Gray Fox, Mar 29, 2010 IP
    Narrator likes this.
  6. Narrator

    Narrator Active Member

    Messages:
    392
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    80
    #6
    Gray Fox's method is definitely more efficient, I didn't think of doing it that way.

    Cheers!
     
    Narrator, Mar 29, 2010 IP