<?php
/**
*
* Counter (v1.1.0)
*
* version:1.1.0
* Author: Village head
* PHP: >4.3
*
*/
class Counter
{
var $CountFolder ;
var $countType ;
var $showtype ;
var $imgArray ;
function Counter ()
{
$this->counttype = 0 ;
$this->showtype = "Getcountbyday" ;
$this->imgarray = Array ();
$this->makecounter ();
}
function makecounter ()
{
$this->countfolder = "log/". Date ("YMD" );
if (! Is_dir ($this->countfolder ))
{
mkdir($this->countfolder,0777 );
}
$this->_docount ();
}
//save Data file
function _docount ()
{
$file = date("YMD"). Ip2long ($_server[' remote_addr ']). ". txt" ;
if (! file_exists ($this->countfolder. "/" . $file ))
{
$fp = @fopen($this->countfolder . "/" . $file , "wb+" );
Here you can save other log records that you need
@fputs($fp,$_server[' REMOTE_ADDR ' ) ]);
@fclose($fp );
}
}
//Get statistics for a specified time
The default is 1 days 0 for all
function getcountbyday($day=1 )
{
//Today's time stamp
$today = strtotime(date("y-m-d" ));
$all = 0 ;
For ($i=$day; $i >= 0 ; $i --)
{
$tsp = $today-(86400*$i );
$folder = "log/". Date ("YMD",$tsp). "/*.txt" ;
$ta = glob("$folder" );
$all= $all+ count($ta );
}
return $all ;
}
function getcountuseimg($day=1 )
{
$allnumber = $this->getcountbyday( $day );
For ($i=0; $i < strlen ($allnumber); $i ++)
{
$idx = substr($allnumber,$i , 1 );
$returnVal. = sprintf(", $this->imgarray[$idx ]);
}
return $returnVal ;
}
function SetStyle($style=',$ext= "gif" )
{
$imgprefix ="img/" ;
$folder = $imgprefix. $style ;
foreach (range(0,9) as $num )
{
$this->imgarray[$num] = $folder . "/" . $num . ". $ext" ;
}
if (is_dir($folder ))
{
$this->showtype = "getcountuseimg" ;
}
Else
{
$this->showtype = "Getcountbyday" ;
}
}
function Show ($day=0 )
{
return $this->{$this->showtype} ($day );
}
}
//Instance counter objects
$counter = new counter ();
//with sports98 style display, tell the default extension is PNG format
$counter->SetStyle("Sports98"), "PNG" );
echo "<br/> Today visit:". $counter -> Show (0 );
//with S1 style display, the default extension is GIF
$counter->SetStyle("S1" ));
echo "<br/> Nearly 3rd visit:". $counter -> Show (2 );
//----counters that display only text
$counter->SetStyle("-" );
echo "<br/> Nearly 3rd visit:". $counter -> Show (2 );
?>