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 !
Related Articles
- Simple Hit CounterUse this simple script for a basic click counter to display the amount of hits to your site.
- Counter in ASPA hit counter is an essential part of a site to know how many people are coming to site. Here is an easy way to make a counter. All you need is access to ASP and be able to give a write access to a directory...
- PHP Live Counter: Online UsersThis is a really great script to use for anyone who is wanting to gain more statistics regarding the number of people who visit your site and so forth. Take a look.
- A Nice Hit CounterI shows
Total Hits Unique Hits Todays Hits Todays Unique Hits - Easy Flat File Hit CounterCreate a very simple hit pageview counter without any database! Use flat file scripting to make an easy counter!
- Hit CounterHit counters are used to track the amount of visitors there have been to a cetain page. I have looked through the internet and I have come up with 2 different ways to produce a hit counter.
- PHP CounterA counter is a simple yet essential script when developing web pages.
First of all, we need a file where to store the data ( a simple text file). This file must be CHMODed to 777 (under Linux)
Lets name this file data.txt. Do not forget to include it into the same directory as the PH... - Tools To Track Your Web Site VisitorsThere are many reasons why a web site owner would want to track the number of visitors to her site, and almost as many ways to do it. This article will compare two well-known tools as a demonstration of the range of options currently available. Site Meters Free Version Site Meter (http://ww....
- Merchant Accounts are SimpleThere are two basic reasons you have a web site. The first is to provide an information type web site. The second is to sell your products and/or services. If you are going to be selling a product or service online you must have the ability to ac...
- Outwards Link CounterThis will allow you to create a script that will count the amount of times a certain link has been clicked and display the results on your page.
