<?php
//Copyright 2002 sanas, mdsa@lycos.co.uk
//This code is distributed for free, but without any warranty
//You should keep the copyright notice intact
//Please send me an e-mail if you use it; enjoy!
//hit counter; you should put this in a file (counter.inc) and include it in your code
//try to give to the class a unique name
class SanasCounter{
//the variable that holds the number of visitors
var $count;
//the name of the file used for storing the number of visitors
var $filename;
//the constructor of the class; it accepts as parameter the filename
//it uses the function getCount() to get the current number of visitors
function SanasCounter ($filename){
$this->filename=$filename;
$this->count=$this->getCount();
}
//this function opens the file which holds the number of visitors
//it reads the counter and returns it's value
//additionaly, it creates the file if it does not exist and initialises the number of visitors to 0
function getCount(){
if (file_exists ($this->filename)){
$fp=fopen ($this->filename,"r");
$temp=fread($fp,filesize($this->filename));
fclose ($fp);
return $temp;
}
//the file does not exists; it is created
else{
$fp=fopen ($this->filename,"w");
fwrite ($fp,'0');
fclose ($fp);
return '0';
}
}
//this function checks whether the cookie used to track the visitors is set or not
//the browser "eats" the cookie until midnight that day; after that, the visitor is "new"
function checkNew(){
//you have to set the cookie variable as global
//if you don't, the class will not work as expected
global $CounterCookieMDS;
if (!isset($CounterCookieMDS)){
//we have a new visitor
$today=getdate();
setcookie ("CounterCookieMDS",'mds count',mktime(0,0,0,$today['mon'],$today['mday']+1,$today['year']));
return true;
}
else{
//unfortunately, the counter will not be incremented
return false;
}
}
//this function updates the counter by opening the file and writing to it
function updateCount(){
$this->count+=1;
$fp=fopen ($this->filename,"w");
fwrite ($fp,$this->count, strlen($this->count));
fclose($fp);
}
//this function returns the number of visitors
//it checks whether the visitor is "new" or not and increments the counter accordingly
function displayCount(){
if ($this->checkNew()){
$this->updateCount();
}
return $this->count;
}
}
//this is a sample class that extends the base class
//it is used for formatting the counter
class FormatSanasCounter extends SanasCounter{
//variable that holds the formated text
var $text;
//the day when the couter has been initialised
var $init;
//the counstructor of the class
function FormatSanasCounter($filename,$init){
$this->SanasCounter($filename);
$this->init=$init;
$temp=$this->displayCount();
$this->text=<<<HTML <div class=counter style="font-size: x-small; background-color: #DCDCDC; align: left">
<span class=counter>$temp</span> visitors since $init<br>
Counter by <a href="mailto:mdsa@lycos.co.uk">sanas</a>
</div> HTML;
}
//this function returns the formated text along with the counter
function formatCounter(){
echo $this->text;
}
} ?>
|
|