Overview
This script can be used to know the number of connected visitors on a given website at a given moment. This concept is simply based on the principle of storing the IP address and the connexion time of each visitor in a MySQL Database.
When a visitor's browser loads the first page, his IP and the time of his connexion (in seconds) are stored. Then, each time he loads a new page the time of his connexion is updated in the Database. Users that did not load a new page during a certain amount of time (5minutes: 300 seconds for instance) will be deleted from the Database..
Then, we count the number of lines in the MySQL tabele to determine how many people are actually browsing the site at this moment (during the last 5 minutes).
toring table structure
Use the following code to create your table using PHPMyAdmin.
create table nb_connected (ip text not null, time bigint(20) not null); |
Put This script on every page of your site
This script must be put in every page of your website.
| <?
//DBase Name: $opt_connected_cfgbase = "base"; //Username : $opt_connected_cfguser = "user"; //PassWord : $opt_connected_cfgpass = "pass"; //Path : $opt_connected_cfghote = "localhost"; // Amout of time to consider a user active $nb_connected_connexion=300; // 5 minutes // Connexion to the database $base_connected=mysql_connect($opt_connected_cfghote, $opt_connected_cfguser,$opt_connected_cfgpass); if (!$base_connected) { ("<center>Error connecting to the Database </center>"); exit(); } // Database selecting if (! mysql_select_db("$opt_connected_cfgbase",$base_connected)) { mysql_close($base_connected); print("<center>Error connecting to the Database</center>"); exit(); } // time $time_connected=date("U"); // Get IP $ip_connected=$REMOTE_ADDR; // Search IP in the DB $query_connected="select * from nb_connected where ip='$ip_connected'"; $result_connected=@mysql_query($query_connected,$base_connected); if (!$result_connected) { mysql_close($base_connected); print("<center>Could not execute request </center>"); exit(); } $nb_connected=@mysql_num_rows($result_connected); if ($nb_connected) { // Update the connexion $query_connected="update nb_connected set time='$time_connected' where ip='$ip_connected'"; $result_connected=@mysql_query($query_connected,$base_connected); } else { // Create connexion $query_connected="insert into nb_connected (ip, time) values ('"; $query_connected.=$ip_connected; $query_connected.="', '"; $query_connected.=$time_connected; $query_connected.="')"; $result_connected=@mysql_query($query_connected,$base_connected); } // Calculate maximum time of connexion $time_max_connected=$time_connected-$nb_connected_connexion; // Deleting Maximum time connexion from DB $query_connected="delete from nb_connected where time<'$time_max_connected'"; $result_connected=@mysql_query($query_connected,$base_connected); // Close connexion to DB mysql_close($base_connected); ?> |
Showing up the number of connected visitors
<?
|
Enjoy the tutorial and counting your live visitors !






