How to count current number of visitors in your website, count website visitors, website analytics, php count website visitors

How to count current number of visitors in your website

Suppose you may be in a situation where you want to track and count the current number of visitors that are accessing or using your website at a specific time. This is mostly made possible by connecting google analytics to your website where it tracks and you can show the real-time number of visitors accessing your website.

Other than google analytics, in this article, I will show you how you can count the current number of visitors in your website without the help of google analytics. You may ask yourself how this is possible, we are going to write a few lines of code and at the end, you will be able to view the visitors at a specific time.

To avoid making your server space consume a lot of space, we shall avoid the MySQL database as we will not want to keep records of users forever but we are going to store the information of users in a file that will keep updating itself after a set period of time and remove details of users whose session have expired.

To begin,

First, create a .txt file in your public directory and name it users.txt

In your server space, in the header section of your website you will add this PHP code below, if you want to track visitors in all the pages in your website add the below code in each file.

<?php
$REMOTE_ADDR = $_SERVER['HTTP_X_FORWARDED_FOR'];
$current_users_file = 'users.txt';
if (!file_exists($current_users_file)) fclose(fopen($current_users_file, "w"));
$users = file($current_users_file);
$found = false;
$user_count = count($users);
$fp = fopen($current_users_file, "w");
foreach($users as $user) {
$user = explode("|", $user);
if ($user[1]+300 < time()) {
$user_count--;
continue;
} elseif ($user[0] == $REMOTE_ADDR) {
$user[1] = time();
$found = true;
}
$user = trim(implode("|", $user))."";
fputs($fp, $user);
}
if (!$found) {
fputs($fp, $REMOTE_ADDR."|".time()."");
$user_count++;
}
fclose($fp);
?>

To explain the code above,

  • 'HTTP_X_FORWARDED_FOR' is set by proxy servers to identify the IP address of the host that is making the HTTP request through the proxy that is, it will get the IP address of the visitor.
  • 'users.txt' is the txt file that will be used to store the session of the users
  • !file_exists to check if the IP received exists in the text file
  • count() is used to get the number of users found in the txt file
  • ($user[1]+300 < time()) to check whether the user has exceeded to set period of 300 seconds or 5 minutes.
  • If that Ip address is not found the txt file in the last 5 minutes it is added to the file by using the fputs($fp, $user) command
  • fclose($fp); closing the txt file

 

With that code and description, you will find that values will be stores in your users.txt file as timestamp values as shown below

How to count current number of visitors in your website

Please note that every user will be represented in his own entry as a timestamp

 

Having known how to save the users details in the users.txt file, it’s now time to display them either in your admin dashboard

To do this, you can create a file in your administrator portal or just include the results in the existing files, for example, you can show the results in the home section of your admin.

The code for displaying the current number of users will be the same as the one for adding but this time we will add the line for echoing the results.

So have the same code and include the section for displaying the result

<?php
$REMOTE_ADDR = $_SERVER['HTTP_X_FORWARDED_FOR'];
$current_users_file = 'users.txt';
if (!file_exists($current_users_file)) fclose(fopen($current_users_file, "w"));
$users = file($current_users_file);
$found = false;
$user_count = count($users);
$fp = fopen($current_users_file, "w");
foreach($users as $user) {
$user = explode("|", $user);
if ($user[1]+300 < time()) {
$user_count--;
continue;
} elseif ($user[0] == $REMOTE_ADDR) {
$user[1] = time();
$found = true;
}
$user = trim(implode("|", $user))."";
fputs($fp, $user);
}
if (!$found) {
fputs($fp, $REMOTE_ADDR."|".time()."");
$user_count++;
}
fclose($fp);
echo 'Online users ' . $user_count . '';
?>

 

The screenshot below shows my result in my admin dashboard with the current number of online users as 1

How to count current number of visitors in your website

With the explanations we have shown above, you can easily show the number of visitors at a specific time in your website.