PHP: Simple Online Users counter

Today I am going to show you how you can create a simple online users counter through php and mysql. For this tutorial you should have some basic knowledge about php and mysql queries.

First of all, you will need a database and a table where we will store information about the online visitors. The table that we will use should have the following 3 fields:

 


  1. id as autonumbe, primary field

  2. ip - varchar(25) to hold the ip address of the online visitors, and

  3. timeout - datetime where we will store the stay time of visitors


Name the table as "tblonlineusers", or if you find it difficult to create table, I've included the sql to create the required table making the task easier for you.

Now, you just need to use the following code, which has instructions in every line:


//Change the below parameters according to your server's config

$host = "localhost";

$username = "root";

$password = "password";

$con = mysql_connect($host, $username, $password); //Connect to MySql server

if (!$con)

{

die('Could not connect: ' . mysql_error());

}

$db = "onlineuserdb"; //Database Name

mysql_select_db($db, $con) or die(mysql_error()); //Select the provided db

//Procedures to add/update online visitors:

$timeout = date("Y-m-d H:i:s" ,time() + (5 * 60)); //The amount of time to keep the visitor online (5 represents 5 minutes, you can change it)

$ip = $_SERVER['REMOTE_ADDR']; //Retrieving ip of the current visitor

$query = "select * from tblonlineusers where ip='$ip'"; //This query will check if this visitor is already in db or not

$result = mysql_query($query) or die("Error 1: " . mysql_error());

if(mysql_num_rows($result) > 0 ) //If yes, then visitor is has opened another page

{

$query = "update tblonlineusers set timeout='$timeout' where ip='$ip'"; //Now just update visitor's record by updating his timeout

}

else

{

$query = "insert into tblonlineusers(ip,timeout) values('$ip', '$timeout')"; //If no record present then insert it into db

}

mysql_query($query) or die("Error 2: " . mysql_error()); //Execute the query

mysql_query("delete from tblonlineusers where timeout < '" . date("Y-m-d H:i:s" ,time()) . "'" ) or die("Error 3: " . mysql_error()) ; //Now delete those visitors whos timeout has elapsed

//Displaying online users:

$result = mysql_query("select count(id) from tblonlineusers");

$row = mysql_fetch_row($result);

$onlineUsers = $row[0];

echo($onlineUsers . " Users Online!"); //You can customize it according to your requirement

?>


View the demo



Tags: php, stats

Download onlineusers.zip

(1.39kb)  188 Downloads

Subscribe to PlanetMaks

PlanetMaks updated regularly with content related to news, tutorials, downloads and many resources. If you don't want to miss out on future posts, you can subscribe by RSS or email.

More in PHP

Related Posts

PHP Email Validation with RegEx Data validation is one of the most important and essential part of any data form. There are many ways to verify data input. Even for email addresses there could be several ways to verify it. In php we can use regex to verify email address. In the following code I've searched for an email address through regex email patteren in the provided value. [...]
Installing PHP5 On Windows Xp Under IIS Its very simple to install PHP5 Under IIS On Windows XP if you dont want to install it manualy. [...]
What is PHP? Hypertext Pre-processor (PHPs) is a server-side scripting language, and server-sidescripts are special commands you must place in Web pages. Those commands are processed before the pages are sent from your Server to the Web browser of your visitor. A typical PHP files will content commads to be executed in the serverin addition to the usual [...]

Most Popular in "PHP"

Installing PHP5 On Windows Xp Under IIS Its very simple to install PHP5 Under IIS On Windows XP if you dont want to install it manualy. [...]
PHP Email Validation with RegEx Data validation is one of the most important and essential part of any data form. There are many ways to verify data input. Even for email addresses there could be several ways to verify it. In php we can use regex to verify email address. In the following code I've searched for an email address through regex email patteren in the provided value. [...]
What is PHP? Hypertext Pre-processor (PHPs) is a server-side scripting language, and server-sidescripts are special commands you must place in Web pages. Those commands are processed before the pages are sent from your Server to the Web browser of your visitor. A typical PHP files will content commads to be executed in the serverin addition to the usual [...]