Page 1 of 1

counting number of visitors to the web site

Posted: Sat May 24, 2003 12:41 am
by jogen143
Hi guys,

I want a script to count number of visitors to my site, can anyone help me out. Thanks in advance.


bye!!

Re: counting number of visitors to the web site

Posted: Sat May 24, 2003 3:13 am
by thewebmushroom
I've written one in Perl if that's any use - the principle could quite easily be converted to PHP.

It records each hit in a CSV file, the name of which is based on an ID you choose plus the month and year (so a new file is generated each month). It records the user's IP, user agent, referer and screen res.

It uses Javascript to catch the Referer because my ISP blocks the HTTP_REFERER variable.

Javascript file "hitScript.js":

Code: Select all

// JavaScript Document

function countMe(myID1) {
	var x = screen.width; var y = screen.height;
	var screenRes = x + 'x' + y;
	if (parent.location.href == self.location.href) {
		myReferrer = document.referrer;
	} else {
		myReferrer = top.document.referrer;
	}
	var query = myReferrer.indexOf('?');
	if (query > 0) {
		myReferrer = myReferrer.substring(0,query);
	}
	if (myReferrer == '') {
		myReferrer = 'noreferrer';
	}
	document.write('<img src="http://YOUR_PATH_HERE/hitCounter.pl?id=' + myID1 + '&referrer=' + myReferrer + '&screen=' + screenRes + '" alt="">');
	return true;
&#125;

function setCountCookie(myID2) &#123;
	var expires = new Date();
	expires.setTime (expires.getTime() + 24*60*60*1000);
	expiresGMT = expires.toGMTString();
	cookieID = myID2;
	document.cookie = "id=" + cookieID + "; expires=" + expiresGMT + "; path=/";
	return true;
&#125;

function checkCountCookie(myID3) &#123;
	var cookieFound = false;
	var cookieString = document.cookie;
	var i = 0;
	while (i <= cookieString.length) &#123;
		start = i;
		end = start + myID3.length;
		if (cookieString.substring(start,end) == myID3) &#123;
			cookieFound = true;
			break;
		&#125;
		i++;
	&#125;
	return cookieFound;
&#125;

cookieFound = checkCountCookie(localID);

if (cookieFound != true) &#123;
	countMe(localID);
	setCountCookie(localID);
&#125;
Perl file "hitCounter.pl":

Code: Select all

#!/usr/bin/perl

BEGIN
&#123;
	use CGI;
	use CGI::Carp qw(carpout fatalsToBrowser set_message) ;
	set_message('<p>An error has occured with this script.</p>') ;
&#125;

$qs = $ENV&#123;'QUERY_STRING'&#125;;
@qs = split(/\&/,$qs);
@qs0 = split(/=/,@qs&#1111;0]);
@qs1 = split(/=/,@qs&#1111;1]);
@qs2 = split(/=/,@qs&#1111;2]);
$id = @qs0&#1111;1];
$referrer = @qs1&#1111;1];
$screen = @qs2&#1111;1];

($sec,$min,$hr,$day,$mon,$yr,$weekday,$doy,$isDST) = localtime(time);
$mon = $mon + 1;
$year = $yr + 1900;

$data_file = $id . '_' . $mon . $year .'_hits.csv';
$hits_file = $id . '_' . $mon . $year .'_hits.txt';
$path = $ENV&#123;'DOCUMENT_ROOT'&#125; . '/YOUR_RELATIVE_PATH/logs';
$data_file = $path . '/' . $data_file;
$hits_file = $path . '/' . $hits_file;
$image = $ENV&#123;'DOCUMENT_ROOT'&#125; . '/YOUR_RELATIVE_PATH/blank.gif';

$remote_ip = $ENV&#123;'REMOTE_ADDR'&#125;;

open(DATA_F, ">>$data_file") || die "Could not open DATA file $data_file: $!";

printf DATA_F ('%04d-%02d-%02d,',$year,$mon,$day);
printf DATA_F ('%02d:%02d,',$hr,$min);
print DATA_F "$remote_ip,$ENV&#123;'HTTP_USER_AGENT'&#125;,$referrer,$screen\n";

print "Content-type: image/gif\n\n";

open (IMAGE, "$image") || die "Could not open image: $!";

binmode IMAGE; 

binmode STDOUT;

print (<IMAGE>);
You need to create a subfolder in the directory you place the scripts called "logs" where the logs will be stored. You need to place a blank image called "blank.gif" in the script directory. Then add the following code into each page of your site:

Code: Select all

<script language="JavaScript" type="text/JavaScript">
<!--
var localID = 'YOUR_ID';
-->
</script>
<script language="JavaScript" src="http://YOUR_PATH_HERE/hitScript.js"></script>
<noscript>
<img src="http://YOUR_PATH_HERE/hitCounter.pl?id=YOUR_ID&referer=noscript" alt="">
</noscript>
Any comments or improvements gratefully received!

Posted: Sat May 24, 2003 12:39 pm
by AVATAr
jogen143, all depends of what kind of functionallity you want: for instance if you want to know how many people go to your site (without checking ip, dates and all that things) you can create a table on your DB called Statistics, with fields: Id, NameOfStat, NumberOfHits


You can have a record : 1 - "Hits"

en use SQL code to increment the hits:

Code: Select all

UPDATE Statistics SET NumberOfHits=NumberOfHits+1 Where NameOfStat="Hits"
this is only an idea you can use. You could store the month and all kind of things, just insert your code in in your initial page.. and thats it