Visitors counter script


Simple visitors counter is a script to record all current visitors and display count of this. We focus on practical, project based training rather than only academic, theoretical parts   .We Provide free demo class . Register for a free demo of the course.

Straightforward Active users counter isn’t going to make use of sources. The details can be saved inside data, although utilizes a unique strategy to normally the one inside different tables: whenever a consumer appointments a webpage in our web page, a record is made inside a “counters” folder. The identify from the record is the IP tackle from the consumer. For each consumer a whole new record is made, plus the customization time from the record will probably match bigger exercise of this unique consumer within the web page. Should the customers is just not a whole new one, the record to be able unique IP tackle will likely be overwritten, consequently a whole new ‘most recent activity’ will likely be noted (which corresponds to the past customization time from the file). Documenting customer’s past exercise isn’t going to call for studying information saved in the database or wording record. php-visitor-counter When keeping track of volume of recent visitors, many data within the “counters” folder tend to be examined. Should the record continues to be designed recently (for illustration within the last 5 units; this parameter can be customizable) the record will likely be paid for as an active/online consumer. However, when the record can be old, will probably be deleted.

 

The task identified for the Active End users counter makes it possible for saving past exercise of people without demanding studying the details within the database. For that reason, the internet marketer might document information in most internet pages within the web page, although the quantity of energetic people could possibly be proven only inside principal web site or in the minimal volume of internet pages, in order that the processing along with read/write exercise will likely be on a internet pages demonstrating the quantity of energetic people.

For that reason, Straightforward Active End users counter could possibly be consider like a uncomplicated (no databases Find Below most frequently used modules in drupal .

For that reason, Straightforward Active End users counter could possibly be consider like a uncomplicated (no databases) along with rapid (mostly intended for internet pages saving energetic people although not demonstrating the number) service, but additionally a small one(only active/online visitors tend to be shown). Internet marketers have to choose no matter whether demonstrating volume of energetic people can be good enough for their sites. If that’s the case, this service is a good alternative.

Requirements and code

To run this service, the following are required:

  • There should create a folder to store temporary files..Inside signal bellow, any haphazard string can be included in the label of the folder.Chances are you’ll develop a folder label “users_EIMvbwhMWa” in the primary directory site of this site, or even go with a various label.
  • A copy associated with document “active_users_record. php” (bellow) must be cloned for the server.The advice is to copy this to be able to main directory site of this site.
  • A copy associated with document “active_users. php” (bellow) must be in addition cloned for the server.Once more, the advice is to copy this to be able to main directory site of this site.
  • Value to get incorporated into the internet pages:
  • In most documents you want to document people (but you may not wish to present the number), the following signal must be involved.<? include($_SERVER[“DOCUMENT_ROOT”]. “/active_users_record. php);? >
  • In most documents you want to document along with present variety of people, the following signal must be involved.<? include($_SERVER[“DOCUMENT_ROOT”]. “/active_users. php);? >
  • Your signal bellow clarifies step-by-step your actions of the software.Your signal seemingly useful, along with inside identical locations substitute alternatives are proven (one of them can be activated).

Code:

File 1:

active_users_record.php
<?

                  
                // ###############  SET UP THE VARIABLES  ########################################

                // FOLDER USED TO STORE TEMPORAL FILES
                //    IMPORTANT:  the folder must have proper permissions  to allow writing files
                //    The name of the temporal files contains the IP  address of the user
                //    ($_SERVER["DOCUMENT_ROOT"] is the root folder for  the website)
                $folder=$_SERVER["DOCUMENT_ROOT"]."/users_EIMvbwhMWa/";

                // ###############  THE WORKING PART OF THE SCRIPT ##############################

                // DO NOT SHOW ERRORS TO VISITORS (just in case)
                error_reporting(0);

                // GET IP ADDRESS OF USER (a function in the bottom is used)
                $ip=getIP();

                // REGISTER THE USER
                //      A file will be created. The name of  the file will contain the IP of the user.
                //      In case the file already exists, it  will be overwritted.
                //      The creation time of the file will  indicate how long ago the user
                //               with this IP visited a page containing this active users counter
                        // OPTION 1, for version 4 or  superior
                        $cf =  fopen($folder.$ip, "w");
                        fwrite($cf, "");
                        fclose($cf);
                        // OPTION 2, for PHP5 or  superior
                        // file_put_contents  ($folder."$ip.txt", "0");

                // ########################  FUNCTIONS  #########################################
                // funtion getIP will be used to get IP address of visitor
                function getIP() {
                        // Option 1 to get the IP  address of visitor
                         //      if a value for  $_SERVER['HTTP_X_FORWARDED_FOR'] is available
                         //      $ip is obtained and returned
                         if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
                                 $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
                                 return $ip;
                        }
                        // Option 2 to get the IP  address of visitor
                         //      if a value for $_SERVER['REMOTE_ADDR']  is available
                         //      $ip is obtained and returned
                         if(isset($_SERVER['REMOTE_ADDR'])){
                                 $ip = $_SERVER['REMOTE_ADDR'];
                                 return $ip;
                        }
                        // IP has not been obtained,  so a default IP is returned
                         //      The default value will be used very  few times, so
                        return "0.0.0.0";
                }

                ?&gt;

File2:

active_users.php

<?

 // ###############  SET UP THE VARIABLES  ######################################## 

                // FOLDER USED TO STORE TEMPORAL FILES
                //    IMPORTANT:  the folder must have proper permissions  to allow writing files
                //    The name of the temporal files contains the IP  address of the user
                //    ($_SERVER["DOCUMENT_ROOT"] is the root folder for  the website)
                $folder=$_SERVER["DOCUMENT_ROOT"]."/<strong>users_EIMvbwhMWa</strong>/"; 

                // TIME SINCE LAST ACTIVITY OF AN USERS TO BE CONSIDERED NON-ACTIVE 
                $timeold=300;   // seconds

                // ###############  THE WORKING PART OF THE SCRIPT ############################## 

                // DO NOT SHOW ERRORS TO VISITORS (just in case)
                error_reporting(0);

                // GET ACTUAL TIME
                $actualtime=date("U");   // seconds since January 1st, 1970.

                // GET IP ADDRESS OF USER (a function in the bottom is used)
                $ip=getIP();

                // REGISTER THE USER
                //      A file will be created. The name of  the file will contain the IP of the user.
                //      In case the file already exists, it  will be overwritted.
                //      The creation time of the file will  indicate how long ago the user
                //               with this IP visited a page containing this active users counter
                        // OPTION 1, for version 4 or  superior
                        $cf =  fopen($folder.$ip, "w");
                        fwrite($cf, "");
                        fclose($cf);
                        // OPTION 2, for PHP5 or  superior
                        // file_put_contents  ($folder."$ip.txt", "0");

                // COUNT NUMBER OF ACTIVE USERS
                //      All files within folder $folder will  be checked
                //      Files $timeold seconds old (defined  above) will be deleted
                //      Files created up to $timeold seconds  ago will be accounted as active users

                        // a counter; no users at  this moment
                        $counter=0;
                        // get the list of files  within $folder
                        $dir = dir($folder);
                        // check all files one by  one (variable $temp will be the name of each file)
                        while($temp =  $dir-&gt;read()){
                                 // the ones bellow are not files, so continue to next $temp
                                 if ($temp=="." or $temp==".."){continue;}
                                 // For real files, get the last modification time
                                 //   (number of seconds since January 1st, 1970)
                                 //    and save the data to variable $filecreatedtime
                                 $filecreatedtime=date("U", filemtime($folder.$temp));
                                 // check whether the file is $timeold seconds old
                                 if ($actualtime&gt;($filecreatedtime+$timeold)){
                                         // the file IS old, so delete it
                                         unlink  ($folder.$temp);                                                                    //
                                 }else{

                                         // the file IS NOT old, so an active user will be accounted
                                         $counter++;
                        }
                }
                // DISPLAY NUMBER OF ACTIVE USERS
                        // Option 1  (displays only the number of active users)
                        //print "Users online: $counter";
                        // Option 2  (displays  number of active users, and defines how old an active user is in seconds).
                        // print "Users online:  $counter (in last $timeold seconds)";
                        // Option 3  (displays  number of active users, and defines how old an active user is in minutes).
                        $minutes=round($timeold/60);
                        print "Users online:  $counter (in last $minutes minutes)";

                // ########################  FUNCTIONS  #########################################
                // funtion getIP will be used to get IP address of visitor
                function getIP() {
                        // Option 1 to get the IP  address of visitor
                         //      if a value for  $_SERVER['HTTP_X_FORWARDED_FOR'] is available
                         //      $ip is obtained and returned
                         if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
                                 $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
                                 return $ip;
                        }
                        // Option 2 to get the IP  address of visitor
                         //      if a value for $_SERVER['REMOTE_ADDR']  is available
                         //      $ip is obtained and returned
                         if(isset($_SERVER['REMOTE_ADDR'])){
                                 $ip = $_SERVER['REMOTE_ADDR'];
                                 return $ip;
                        }
                        // IP has not been obtained,  so a default IP is returned
                         //      The default value will be used very  few times, so
                        return "0.0.0.0";
                }

                ?&gt;

Online Web Development Training,Video Tutorials