Hi guys,
I want a script to count number of visitors to my site, can anyone help me out. Thanks in advance.
bye!!
counting number of visitors to the web site
Moderator: General Moderators
-
thewebmushroom
- Forum Newbie
- Posts: 3
- Joined: Fri May 23, 2003 10:47 am
- Location: Yorkshire, UK
Re: counting number of visitors to the web site
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":
Perl file "hitCounter.pl":
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:
Any comments or improvements gratefully received!
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;
}
function setCountCookie(myID2) {
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;
}
function checkCountCookie(myID3) {
var cookieFound = false;
var cookieString = document.cookie;
var i = 0;
while (i <= cookieString.length) {
start = i;
end = start + myID3.length;
if (cookieString.substring(start,end) == myID3) {
cookieFound = true;
break;
}
i++;
}
return cookieFound;
}
cookieFound = checkCountCookie(localID);
if (cookieFound != true) {
countMe(localID);
setCountCookie(localID);
}Code: Select all
#!/usr/bin/perl
BEGIN
{
use CGI;
use CGI::Carp qw(carpout fatalsToBrowser set_message) ;
set_message('<p>An error has occured with this script.</p>') ;
}
$qs = $ENV{'QUERY_STRING'};
@qs = split(/\&/,$qs);
@qs0 = split(/=/,@qsї0]);
@qs1 = split(/=/,@qsї1]);
@qs2 = split(/=/,@qsї2]);
$id = @qs0ї1];
$referrer = @qs1ї1];
$screen = @qs2ї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{'DOCUMENT_ROOT'} . '/YOUR_RELATIVE_PATH/logs';
$data_file = $path . '/' . $data_file;
$hits_file = $path . '/' . $hits_file;
$image = $ENV{'DOCUMENT_ROOT'} . '/YOUR_RELATIVE_PATH/blank.gif';
$remote_ip = $ENV{'REMOTE_ADDR'};
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{'HTTP_USER_AGENT'},$referrer,$screen\n";
print "Content-type: image/gif\n\n";
open (IMAGE, "$image") || die "Could not open image: $!";
binmode IMAGE;
binmode STDOUT;
print (<IMAGE>);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>- AVATAr
- Forum Regular
- Posts: 524
- Joined: Tue Jul 16, 2002 4:19 pm
- Location: Uruguay -- Montevideo
- Contact:
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:
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
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"