PHP Implements counter function

Source: Internet
Author: User


<?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 );
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.