Author Topic: How to get Visitors online  (Read 3252 times)


  • Newbie
  • *
  • Posts: 1
    • View Profile
How to get Visitors online
« on: June 16, 2009, 08:55:22 AM »
I go to
newbielink: [nonactive]

near the bottom there is

  ###  visitors online

How do you get the number of Visitors online ?



  • Administrator
  • Hero Member
  • *****
  • Posts: 2113
    • View Profile
Re: How to get Visitors online
« Reply #1 on: June 16, 2009, 09:10:44 AM »

We record each unique IP adddress that visits us and store it in a table along with a time and date stamp.

On each page load we do a count of the addresses in the table to get that number.

Also on each page load any IP addresses that are more than 10 minutes old are flushed so they don't get counted. The number you see is the number of different visitors in the last ten minutes.

If you'd like the code for this function, I've pasted a similar function in below.

I go to

near the bottom there is

  ###  visitors online

How do you get the number of Visitors online ?

Code: [Select]
This script displays number of users currently browsing your site.

The table structure:

CREATE TABLE useronline (
timestamp int(15) DEFAULT '0' NOT NULL,
ip varchar(40) NOT NULL,
file varchar(100) NOT NULL,
PRIMARY KEY (timestamp),
KEY ip (ip),
KEY file (file)

The actual Users Online script below should be included in the
footer (or on all pages). It will display the numbers of users
on your website at any one time.

$db_user "xxx";
$db_pass "xxx";
$database "xxx";

$db mysql_connect($server$db_user,$db_pass);

$timeoutseconds 600//10 minutes

$timestamp time();
$timeout $timestamp-$timeoutseconds;

$insert mysql_query("INSERT INTO useronline VALUES ('$timestamp','$REMOTE_ADDR','$PHP_SELF')",$db);
$insert)) &#123;
print "Useronline Insert Failed > ";
$delete mysql_query("DELETE FROM useronline WHERE timestamp<$timeout",$db);
$delete)) &#123;
print "Useronline Delete Failed > ";
$result mysql_query("SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'",$db);
$result)) &#123;
print "Useronline Select Error > ";
$user mysql_num_rows($result);

$user == 1) &#123;
print("$user user online\n");
#125; else &#123;
print("$user users online\n");
« Last Edit: June 16, 2009, 09:20:37 AM by Mike »