PHP script to implement online user counter and page views

PHP script, free and ready to use that shows a counter of the users connected to the website, those who are online at the same time, and the page views that the web has today.

In this article I offer a quick solution to anyone who wants to implement a message on a website, so that the visitor can know a couple of data about the traffic of the page. All in a free way, with a GPL license, and in a simple way, since we do not need to configure elements such as a database.

This script will create a message on any site on the page that will be seen by users who are visiting the site at any given time. On the other hand, it will report the page views that have occurred today.

Characteristics and differences of this PHP script with others on the Internet

On the websites of the ASOCAE Educational Association, with which I collaborate as an amateur webmaster, I missed a counter for visits and page views, apart from the usual statistical services, which I could surely obtain from various companies that offer that service, even for free.

In these cases the needs were simpler: it consisted of a simple counter of the unique users found online, and a counter that could show the number of page views during the day and during the month. And all this, without graphic flourishes, showing the results with text characters and at most accompanied by a couple of small identifying icons.

This would allow users who visit the web to make an evaluation of the popularity of the site at a glance, based on the number of users and views that are registered.

However, nothing I found on the Internet met these requirements 100%. In some cases, a database was required, something that seemed excessive to me for the accountant’s claims; in other cases, server calls were abused in the code, consuming excessive resources; and in no case did I find something that served the three needs as a whole (although there were plenty of separate codes). So I dedicated myself to taking for my design the best of each one of the scripts that I was finding on the net.

See also  Multiple background images with CSS3

After a testing period of one month on the ASOCAE websites and blogs, which I consider satisfactory, I have decided to release it and make it available to the public, under a GPL license, so that you can use it if you like, or modify it according to your needs if you like.

Implementation of the counter of online users and page views

The counter is simple, in text format, but fast and functional. In the following image you can see the text produced by the counter, once inserted into the page.

The script is written in PHP language and uses text files instead of a database to store the posted data. They are actually three counters in one. The first one shows the unique users who are visiting the web (IPS’s are captured and the exact moment in seconds when they accessed), the second and third counters show the page views per day and per month, respectively. At the end of the day and/or month, they are reset to zero and start over.

The script is hosted on the server in a folder called “visits” (you can access the download link below). You have to transfer the entire folder to the root of the server, and configure a correct set of permissions, along with the user who owns the files, any that Apache is able to write.

In the download file, you will find a series of files that you could place in a folder called “visits”. Contains the following files:

– users.php (php script that manages the unique users that are online)

contar_usuarios.dat (Text file where the data of the users who are online are saved)

– visits.php (php script that manages the visits that are received)

contar_dia.dat (Text file where the number of visits during the day is saved)

contar_mes.dat (Text file where the number of visits during the month is saved)

– dia.dat (Text file where the day of the current date “from 1 to 31” is saved)

– mes.dat (Text file where the month of the current date “from 1 to 12” is saved)

– logo_useronline.gif (online users icon)

– logo_visitas.gif (icon for page views)

See also  Create transparent layer in photoshop

Important: all files with the .dat extension must be initialized with the value “0” (and without containing any blank spaces). Edit them previously to verify that they have that value, otherwise write a “0” on them and save them before transferring them to the server.

Because it is of special interest to understand the operation of the scripts (for those who are interested in knowing the ins and outs of the programming I have done), I leave you the codes of the two main scripts (users.php and visits.php), with their comments:

users.php

$session) { $exists=1; } if(trim($ele) > $session) { $array=implode(“:”,$ele); } } //Open the file to save the new data. //A loop is created to loop through the file and read its contents $p=@fopen($file,”w+”); if($exists == 0){ $array=$time.”:”.$ip.”n”; } foreach($array as $eoeo){ $record.=trim($eoeo).”n”; } @fwrite($p,$write); @fclose($p); $con=@file($file); //The number of unique users visiting the web is saved in a variable $n_usuarios=count($con); //Show formatted data in red echo “

$n_users online users

“; ?>

visits.php

Page views today: $sum_day, Month: $ sum_month

“; ?>

See also  Remove the blue box from an image that is a link

To visualize the data I use two IFRAMES:

We have to insert the IFRAME code in all the pages that we want to count, and placing them where we want to show the results. Obviously, if we insert the code only on the main page, this will be the only page counted.

Each developer will know which method to use to count all the pages of the site, for example, inserting the IFRAME in a single file and calling it by means of a PHP “include” from all the pages of the site with something like

Loading Facebook Comments ...
Loading Disqus Comments ...