Location: PHPKode > scripts > phpHitCounter > phphitcounter/hitcounter.class.php
<?php
/**
* Site Hit Counter
* @author Vedanta Barooah <hide@address.com>
* @package phpHitCounter
* @version 1.0.1
*/

/**
* Class to create a image or text based hitcounter
*/
class hc{
    	/**
    	* File which stores the site hits
    	* @var string
    	* @access public
    	*/
    	var $fileName		=	NULL;

    	/**
    	* File which stores the ip of the visitors
    	* @var string
    	* @access public
    	*/
    	var $visitorFile	=	NULL;

    	/**
    	* Allow only unique visitors ?
    	* @var boolean
    	* @access public
    	*/
	var $uniqueHit		= 	false;

	/**
	* Set the mode in which the counter is displayed : image or text
	* @var string
	* @access public
	*/
	var $counterMode	=	'text';		/* alternate is image */

    	/**
	* Set the type of image to be used for the hit counters
	* @var string
	* @access public
	*/
	var $imageType		=	'gif';		/* alternate is png, jpg or jpeg */

	/**
	* Set the image path
	* @var string
	* @access public
	*/
	var $imagePath		=	'./images/';

	/**
	* Set the counter theme, to be used only with the image mode
	* @var string
	* @access public
	*/
	var $counterTheme	=	'default';

    /**
    * Class constructor
    *
    * @param string $counterFile
    * @param boolean $forceCreate
    * @access public
    */
	function hc($counterFile,$forceCreate=false,$visitFile=NULL){
		if($forceCreate){
			if(!file_exists($counterFile)) touch($counterFile);
			if($visitFile)
                if(!file_exists($visitFile)) touch($visitFile);
		}
		$this->fileName	= $counterFile;
		$this->visitorFile = $visitFile;
	}

    /**
    * Visitors should be unique or not?
    *
    * @param boolean $unique_visitors
    * @access public
    */
    function uniqueVisitor($unique_visitors=false){
    	$this->uniqueHit	=	$unique_visitors;
	}

	/**
	* Set the counter mode, ie. image or text
	*
	* @param string $mode
	* @param string $imageFormat
	* @param string $imagePath
	* @access public
	*/
    function setCounterMode($mode='text',$imageFormat='gif',$imagePath='./images/'){
    	$this->counterMode	=	$mode;
        $this->imageType	=	$imageFormat;
        $this->imagePath	=	$imagePath;
	}

	/**
	* Search for an old visitor
    *
    * @param string $ip
	* @access private
	*/
	function searchRevisit($ip){
		$x=split("\r\n",file_get_contents($this->visitorFile));
		$y=array();
        for($idx=0;$idx<count($x);$idx++){
        	$t=explode('|',$x[$idx],2);
        	array_push($y,$t[0]);
		}
		if(in_array($ip,$y)) return true;
		else return false;
	}

	/**
	* Register a site hit
	*
	* @access public
	*/
    function hitSite(){
    	$hits = file_get_contents($this->fileName);
    	if(!$fp=fopen($this->fileName,"w")) die("Cant' write to file");
    	if($hits=='' || $hits==0) $newHit	=	1;
    	else{
       		if($this->searchRevisit($_SERVER['REMOTE_ADDR']) && $this->uniqueHit)
      			$newHit = $hits;
      		else
      			$newHit = $hits+1;
		}
        fwrite($fp,$newHit);
        fclose($fp);
        /* write the ip of the visitor to the file */
        if($this->visitorFile != NULL ){
        $vfp=fopen($this->visitorFile,"a");
        $visit_data = $_SERVER['REMOTE_ADDR'] . '|' . $newHit . '|' . date("d/m/Y") . '|' . time().'|'."\r\n";
        # echo $visit_data;
        fwrite($vfp,$visit_data);
        fclose($vfp);
		}
    }

    /**
    * Set the counter theme in case of image mode
    *
    * @access public
    * @param string $themeName
    */
    function setTheme($themeName='default'){
    	$this->counterTheme=$themeName;
	}

    /**
    * Display the counter
    *
    * @access public
    */
    function showCounter(){
	    	$hits = file_get_contents($this->fileName);
	    	if($this->counterMode=='text'){
	    		/* show a text hit counter */
	            echo $hits;
			}
			if($this->counterMode=='image'){
				/* display an image hit counter */
	            $hits_array=preg_split('//', $hits, -1, PREG_SPLIT_NO_EMPTY);
				for($idx=0;$idx<count($hits_array);$idx++)
				echo "<img src='". $this->imagePath.$this->counterTheme."/".$hits_array[$idx].'.'.$this->imageType."' border=0>";
			}
	}



}
/* end of class */
?>
Return current item: phpHitCounter