Location: PHPKode > scripts > JPCounterClass > jpcounterclass/class.php
<?php

include "mysql.class.php";
$cnt = new Counter; //Initiate the class

// Change your login information in mysql.class.php
$db = new database();

class Counter
{

	var $imgTpl = "*_base.png"; //8 replaced with color name, lowercase
	var $defaultDays = 5;
	var $data = array();
	
	function Hit( $svr , $dohit = true) //Pass $_SERVER
	{
		global $db;

		if( $svr['REMOTE_ADDR'] == "" ) { $svr = $_SERVER; }
		
		$this->data = $svr;
		$fname = $svr['SCRIPT_FILENAME'];
		$date = time();
		$ip = $svr['REMOTE_ADDR'];	

		if ($dohit)
		{
			$db->query("INSERT INTO counter (filename,visitor,time) VALUES ('{$fname}','{$ip}','{$date}')");		
		}
	}
	
	function Total()
	{
		global $db;
		$db->query("SELECT COUNT(*) FROM counter");
		$ar = $db->get_row();
		$count = $ar[0];	
		return $count;
	}
	
	function Page() //total for this page
	{
		global $db;		
		$db->query("SELECT COUNT(*) FROM counter WHERE filename='{$this->data['SCRIPT_FILENAME']}'");
		$ar = $db->get_row();
		$count = $ar[0];	
		return $count;		
	}
	
	function Today()
	{
		global $db;

		//get total seconts for today, and - it from time
		$h = date("G") * 60 * 60;
		$m = date("i") * 60;
		$s = date("s");

		$total = $h + $m + $s;
		$now = time();
		$start = $now - $total;
			
		$db->query("SELECT COUNT(*) FROM counter WHERE time>=$start");
		$a = $db->get_row();
		$num = $a[0];
			
		return $num;	
	}
	
	function Days($days) //not including today, so 5 days, from midnight this day
	{
		global $db;		
		
		//get total seconts for today, and - it from time
		$h = date("G") * 60 * 60;
		$m = date("i") * 60;
		$s = date("s");

		$total = $h + $m + $s;
		$now = time();
		$start = $now - $total;
			
		//fix up the number of days
		if ($days == "") { $days = $this->defaultDays; }
		$days= abs($days); //remove negative, could mess up
		
		$addt = $days * 24 * 60 * 60; 
		$start -= $addt; // take off the days
		
		$db->query("SELECT COUNT(*) FROM counter WHERE time>=$start");
		$a = $db->get_row();
		$num = $a[0];
			
		return $num;		
	}	
	
	function TodayPage()
	{
		global $db;

		//get total seconts for today, and - it from time
		$h = date("G") * 60 * 60;
		$m = date("i") * 60;
		$s = date("s");

		$total = $h + $m + $s;
		$now = time();
		$start = $now - $total;
			
		$db->query("SELECT COUNT(*) FROM counter WHERE time>=$start AND filename='{$this->data['SCRIPT_FILENAME']}'");
		$a = $db->get_row();
		$num = $a[0];
			
		return $num;	
	}	
		
	function DaysPage($days) //not including today, so 5 days, from midnight this day
	{
		global $db;		
		
		//get total seconts for today, and - it from time
		$h = date("G") * 60 * 60;
		$m = date("i") * 60;
		$s = date("s");

		$total = $h + $m + $s;
		$now = time();
		$start = $now - $total;
			
		//fix up the number of days
		if ($days == "") { $days = $this->defaultDays; }
		$days= abs($days); //remove negative, could mess up
		
		$addt = $days * 24 * 60 * 60; 
		$start -= $addt; // take off the days
		
		$db->query("SELECT COUNT(*) FROM counter WHERE time>=$start AND filename='{$this->data['SCRIPT_FILENAME']}'");
		$a = $db->get_row();
		$num = $a[0];
			
		return $num;		
	}
	
	function UniqueTotal()
	{
		global $db;		
		$db->query("SELECT COUNT(DISTINCT visitor) FROM counter");
		$ar = $db->get_row();
		$count = $ar[0];	
		return $count;				
	}
	
	function UniquePage()
	{
		global $db;		
		$db->query("SELECT COUNT(DISTINCT visitor) FROM counter WHERE filename='{$this->data['SCRIPT_FILENAME']}'");
		$ar = $db->get_row();
		$count = $ar[0];	
		return $count;			
	}
	
	function UniqueToday()
	{
		global $db;

		//get total seconts for today, and - it from time
		$h = date("G") * 60 * 60;
		$m = date("i") * 60;
		$s = date("s");

		$total = $h + $m + $s;
		$now = time();
		$start = $now - $total;
			
		$db->query("SELECT COUNT(DISTINCT visitor) FROM counter WHERE time>=$start");
		$a = $db->get_row();
		$num = $a[0];
			
		return $num;	
	}
	
	function UniqueDays($days)
	{
		global $db;		
		
		//get total seconts for today, and - it from time
		$h = date("G") * 60 * 60;
		$m = date("i") * 60;
		$s = date("s");

		$total = $h + $m + $s;
		$now = time();
		$start = $now - $total;
			
		//fix up the number of days
		if ($days == "") { $days = $this->defaultDays; }
		$days= abs($days); //remove negative, could mess up
		
		$addt = $days * 24 * 60 * 60; 
		$start -= $addt; // take off the days
		
		$db->query("SELECT COUNT(DISTINCT visitor) FROM counter WHERE time>=$start");
		$a = $db->get_row();
		$num = $a[0];
			
		return $num;		
	}
	
	function UniqueTodayPage()
	{
		global $db;

		//get total seconts for today, and - it from time
		$h = date("G") * 60 * 60;
		$m = date("i") * 60;
		$s = date("s");

		$total = $h + $m + $s;
		$now = time();
		$start = $now - $total;
			
		$db->query("SELECT COUNT(DISTINCT visitor) FROM counter WHERE time>=$start AND filename='{$this->data['SCRIPT_FILENAME']}'");
		$a = $db->get_row();
		$num = $a[0];
			
		return $num;	
	}	
		
	function UniqueDaysPage($days) //not including today, so 5 days, from midnight this day
	{
		global $db;		
		
		//get total seconts for today, and - it from time
		$h = date("G") * 60 * 60;
		$m = date("i") * 60;
		$s = date("s");

		$total = $h + $m + $s;
		$now = time();
		$start = $now - $total;
			
		//fix up the number of days
		if ($days == "") { $days = $this->defaultDays; }
		$days= abs($days); //remove negative, could mess up
		
		$addt = $days * 24 * 60 * 60; 
		$start -= $addt; // take off the days
		
		$db->query("SELECT COUNT(DISTINCT visitor) FROM counter WHERE time>=$start AND filename='{$this->data['SCRIPT_FILENAME']}'");
		$a = $db->get_row();
		$num = $a[0];
			
		return $num;		
	}	
	
	function AddImage($color = 'rand')
	{
		$ok = array('red','orange','yellow','green','blue','purple','aqua');
		
		$color = strtolower($color);
		
		//can use a number too
		if($color > 0 AND $color < count($ok)+1)
		{
			$col = $ok[$color-1];	
		
		} elseif ( in_array($color, $ok) )
		{
			$col = $color;
		} elseif ( $color == 'rand' )
		{
			$col = $ok[ rand(0,6) ];
		}
		{
			$col = 'blue';	
		}

		$num = $this->Total();
		$file = str_replace("*", $col, $this->imgTpl);
		$att = base64_encode("{$col}:{$num}:{$file}");	
		$html = "<img src='./counter_img.php?data={$att}' alt='Total Hits: {$num}' width='132' height='43'>";
		
		return $html;		
	}
		
}

?>
Return current item: JPCounterClass