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:
- 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.
- 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! ;)
|