Free Web Counter Script

This free web counter script tracks vistors to any of your web pages with one simple web counter script that writes the results to a text file for easy viewing of your stats.

It records total number of hits, time stamp of hit, ip address of visitor and referring page.

The web counter blocks direct access from external sites.

You need PHP installed on your web hosting for it to work.

Free Web Counter Code

Below is the code listing. Copy the web counter code into a file called "webcounter.php".

The first few lines allow you to change some settings such as:

Save the webcounter.php file in a directory called "log" on your server. Set the file permissions of the log directory to '777' i.e. with the chmod 777 command. This allows the script to create the log file and write to it.

<?php
//Created by andyw@urgentclick.com
//Licensed under terms of the GNU General Public License version 2+
$maxlines = 100; // the maximum number of lines to show
$fn = "log.txt"; // the name of the log file to create
$s = 'document.write(\'&copy; 2005 All Rights Reserved\')';
$host = 'yoursite.com'; // the shortest url

// get referring page
$page = @$HTTP_REFERER;
$uarray = parse_url($page);
$refhost = $uarray["host"];

// check referring page is on this site
if ( strpos($refhost, $host) === false ) {
  exit ("Access denied from: $refhost");
}

// create new file if necessary
if (!file_exists($fn)) {
   $fp = fopen ($fn, "w");
   $os = "0\n";
   $fw = fwrite($fp, $os);
   fclose($fp);
}

// read existing data
$fa = file($fn);
$nl = count($fa);

$c = $fa[0] + 1; // inc hit

// write data to file
$fp = fopen ($fn, "w");
if (flock($fp, LOCK_EX)) { //lock the file
  $os = $c . "\n\n";
  $fr = fwrite($fp,$os);
  $ip = getenv("REMOTE_ADDR");
  $os = $uarray["path"]."\t".date("D M jS g:ia")."\t$ip\t".$_GET['ref'];
  $fr = fwrite($fp,$os);
  if ($nl > $maxlines) { $nl = $maxlines; }
  if ($nl > 1) {
     for ($i = 1; $i < $nl; $i++)
   	    $fr = fwrite($fp,$fa[$i]); 
  }
	flock($fp, LOCK_UN); //release lock
  fclose($fp);
}

//output the javascript text
echo ($s);
?>

Using the Web Counter

When the web counter is in the log directory it is ready to be used. The way it works is by a line of javascript on the web page which calls the web counter, the web counter then records the hit data in the text file and returns some text for display on the web page.

Here is the javascript code to add to your webpages:

Insert this code in .php pages (it includes referring page tracking):
<script language="javascript" type="text/javascript" src="/log/webcounter.php?ref=<?php echo @$HTTP_REFERER; ?>"></script>

Insert this code in html pages (no referring page information is recorded):
<script language="javascript" type="text/javascript" src="/log/webcounter.php"></script>

Here is a trick to get the referral information on an html page. It uses javascript and an inline frame that hides the text in a tiny window:
<SCRIPT TYPE="text/javascript" LANGUAGE="javascript">
var counterPath = "log/webcounter.php";
document.write("<iframe src=\""+counterPath+"\?ref="+document.referrer+"\" width=1 height=1 marginwidth=0 marginheight=0 frameborder=0 scrolling=\"no\"><\/iframe>");
</SCRIPT>

Tips:

It is easy to have multiple counters for different pages by using different names for the webcounter.php file and the log files.

Web Counter License

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

http://www.gnu.org/licenses/gpl.html

logo
Webmaster Tools & Resources
Blog