Keeping Track Of Visitors With PHP


I keep track of everything that happens with my website. The IIS web server has a logging feature that allows you to control what you log; I log everything it allows me to. Unfortunately everything I do on the website is captured by the logs, too. I wrote a simple Java program that strips out records I create in the logs:

 

import java.io.*;
class NotMe
{
   public static void main(String[] args)
   {
      try
      {
      String newLine = System.getProperty("line.separator");
      FileInputStream fstream = new FileInputStream(args[0]);
      DataInputStream in = new DataInputStream(fstream);
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
      
      FileWriter ostream = new FileWriter(args[1]);
      BufferedWriter out = new BufferedWriter(ostream);     
      String newline = br.readLine();
      String oldline = newline;
      out.write(newline);
      out.write(newLine);   
      int strPos = 0;  
      while ((newline = br.readLine()) != null)
      {        
               strPos = newline.indexOf("127.0.0.1", 0);
               if ((newline.indexOf("127.0.0.1", strPos) == -1) && (newline.indexOf("73.49.60.32", 0) == -1))
               {
                  out.write(newline);
                  out.write(newLine);                   
               }
      }
      out.close();
      in.close();
      }
      catch (Exception e) {};
   }
}
You may have noticed from the above code that I check for the first occurrence of the string "127.0.0.1" and start my search after that point. Every line in the log starts with 127.0.0.1 (not the beginning of the line, but the first IP address per record in the log), which is the "loopback" IP address of every computer; i.e., the computer can call itself by the address 127.0.0.1.  If the Java program sees a second occurrence of 127.0.0.1 or the WAN address of this computer (73.49.60.32) then this record was a hit on my website by my own PC. This greatly reduces the number of records for me to sift through, but doesn't tell me who visits my site the most, or which page on my site is the most popular. 

I recently started checking on the SEO information for my website that is generated by Bing; https://www.bing.com/webmaster/home  and it does tell me all sorts of interesting things; e.g., how many links point to my site, how many hits does each page have, etc.  It really does give me all the info I was looking for, but I thought about adding some code to my simple Java program to get that info, too. Wait a second! If I can already get the info off of the Internet, why am I thinking of writing code to get it? Actually, I find writing stuff like that interesting, and in the past I would've done just that. Nowadays I have to think about other uses of my time; e.g., writing blogs, doing things my wife yells at me to do, etc. So I googled for a program that analyzes IIS logs; Web Log Expert Lite. Here's a screenshot of a page produced by Web Log Expert Lite:

 

 
Web Log Expert Lite also gives hit information for each page on my site, the percentage of hits by a particular browser, etc. So I'm all set, right? No, I want the web page to tell me how many hits have happened. This is pretty easy to do, because in the body tag of my home page I have an "onload" action. Onload action? An onload action tells your web page what to do when it is fully loaded into a browser. In this particular case I'd have my web page call some script (PHP, Perl, ASP, NodeJS, etc.) that would open up a file, read a record in it that holds the number of hits that have already happened, add one to it, update the number of hits in the file (by writing over the current value with the one we just inced by 1), close the file and probably write the number of hits someplace on the web page. I didn't have to write the code for this script; a bunch have already been written. 

I downloaded a PHP script from the Internet that counts hits the way I just described, and stuck it into my home page as an include file; SSI (Server Side Include). SSI files are read in by your web server (hence the term "Server Side") concatenated to the web page the web server is already dealing with, and then sent to the web client as one web page. The form of the SSI statement is like an HTML comment; <!--#include file="HitCounter.php" -->  Unfortunately this doesn't work. My homepage is an HTML5 web page, and the IIS web server I use doesn't know about PHP inside of HTML files. The IIS server does know how to deal with PHP code in web pages that end with .php  It's possible to tell the IIS server how to handle PHP inside of HTML files, but then you wind up using the PHP preprocessor to handle every web page that's loaded into your web server; way too much overhead.

If you rename your home page from index.html to index.php then you can either copy and paste the counter script into your home page or you can include it with the PHP include statement; <?php include("HitCounter.php"); ?>  I used the PHP include statment and now you can see the number of hits on my Personal Home Page listed at the top of the page (the include statement comes right after the body tag.  You can view the source of my Personal Home Page and search for "php" and "include" to see just how I did this;  http://community-info.org/index.php  



Return To My Blog Page                                              Return To My Programming Page