aheadWorks - Web Development Company
aheadWorks - Be ahead of your competitors!
home contact site map
What's New:
aheadWorks - Web Development Company
Company Solutions Services Price-Quote Projects X-Cart

HomeWeb Development ArticlesTutorial: How many people are browsing my site now?

Tutorial: How many people are browsing my site now?

We are sure that everyone saw such popular thing as "N people on the site now". This tutorial shows one of the way of creating "N people on the site now" message using PHP and MySQL.

First of all we need a database and table "session" with two fields in it: "username" and "time".

CREATE TABLE session(
  username varchar(25) DEFAULT '' NOT NULL,
  time varchar(14) DEFAULT '' NOT NULL
);

Now we can write a PHP script. The algorithm is quite simple. We will identify each user and mark the time he/she accessed a page. That's all.

function online(){
  $username = getenv("REMOTE_ADDR");
  $past = time() - 900;
  // This string removes all expired records from
  // the database
  mysql_query("DELETE FROM session WHERE time < $past");
  // Retrieve all records with specified REMOTE_ADDR
  $result = mysql_query("
    SELECT time FROM session
    WHERE username='$username'
  ");
  $ctime = time();
  // If there was such record in the database then renew it
  // and set new access time, if not - add a new record.
  if ($row = mysql_fetch_array($result)){
    mysql_query("
      UPDATE session
      SET username='$username', time='$ctime'
      WHERE username='$username'
    ");
  }
  else{
    mysql_query("
      INSERT INTO session (username, time)
      VALUES ('$username', '$ctime')
    ");
  }
  $result = mysql_query("SELECT COUNT(*) FROM session");
  // Count number of records in the database - it is a number of users
  // on your site
  $count = mysql_num_rows($result);
  return $count;
}

This method is fast and simple. But users are tracked by IP only. Why is it bad? Let's see:

  1. Some providers force their clients to use proxy-servers. What will be in REMOTE_ADDR then? It's obvious - address of proxy-server. And what if we have several users from the same provider? The script will count them as a single user.
  2. Some providers changes IP address of a user from time to time or gives dynamic IP's.

We must use cookies... Damn!

We need new table "www_online":

CREATE TABLE www_online(
  hid int(11) NOT NULL auto_increment,
  sess_id varchar(25) DEFAULT '' NOT NULL,
  last_time varchar(14) DEFAULT '' NOT NULL,
  PRIMARY KEY (hid)
);

Where sess_id - session id;
last_time - last time user accessed the site.

And now the function:

function online(){
  session_start();
  // Set cookie life-time
  session_set_cookie_params("0");
  $id = session_id();
  $time = time();
  $past = time() - 900;
  // Delete expired sessions
  mysql_query("DELETE FROM www_online WHERE last_time < '$past'");
  $result = mysql_query("
    SELECT last_time
    FROM www_online
    WHERE sess_id='$id'
  ");
  // This makes 1 if there are sess_id in a table, else 0
  $rows = mysql_num_rows($result);
  if ($rows != "0"){
    mysql_query("
      UPDATE www_online
      SET last_time='$time'
      WHERE sess_id='$id'
   ");
  }
  else{
    mysql_query("
      INSERT INTO www_online (last_time, sess_id)
      VALUES ('$time', '$id')
    ");
  }
  $result = mysql_query("SELECT * FROM www_online");
  $count = mysql_num_rows($result);
  return $count;
}

Finally we've got it! Now you can insert a simple script that will display the calculated amount of visitors on your page. Enjoy! ;)


Buy Now Templates



Hot Services

Dedicated Team

Dedicated Team is a group of programmers and designers completely at your disposal. You can set them any tasks, give any projects, develop one or more web projects of any complexity in the same time and perform other activities. This is like having your own people employed on a time period you need, with the only exception you don't have to deal with all this hassle of looking for right persons, settle space etc. This is a remote team.

learn more


X-Cart Special Offer

aheadWorks is proud to announce the start of our unique x-cart promotion offer.
In short: aheadWorks offers you promotion of your products from your online shop.

learn more


X-Cart Customization Services

X-Cart services that can be implemented by aheadWorks:

learn more



Company Solutions Services Price-Quote Projects X-Cart Contact

aheadOffice — Web 2.0 online groupware  ::  X-Cart Customization Services  ::  Buy Now Template Store

© 2003-2008 aheadWorks Co. All rights reserved.