[HIT COUNTER] Need coding for one.

Newbie Spellweaver
Joined
Apr 22, 2010
Messages
24
Reaction score
2
Hi there, I have tried to use the websites that calculate it for you, and i have clicked on 'Calculate Unique Visits Only', but everyone seems to count page visits. I have tried numerous of these websites but cant seem to find one.

Is there anyone out there who knows one that works 100% only for unique visits - or that can code me one? (I dont know how these things work im a n000b. ^^)

Thanks :thumbup:
 
yeah.. i cant do that, cos i suck :(

well google is your friend, its really really really simple.


here, i'll start you off.

create a new mysql table called 'hits'

give it 2 columns, ID (primary key) and IP.

the ID will store the hit number, it will increment automatically for us, because we set it as the primary key, the IP will store each unique ID.

at the bottom (or top, depending on how aggressive an analytics setup you want), put a snippet of php which connects to the mysql database, selects the 'hits' table and checks if theres a row which has the user's IP, if it returns 0 rows, add a new row with the user's IP in the 'IP' field, don't set the ID manually.


To see how many unique hits you've had, count your rows.



if you're lazy like me... Google Analytics | Official Website
 
well google is your friend, its really really really simple.


here, i'll start you off.

create a new mysql table called 'hits'

give it 2 columns, ID (primary key) and IP.

the ID will store the hit number, it will increment automatically for us, because we set it as the primary key, the IP will store each unique ID.

at the bottom (or top, depending on how aggressive an analytics setup you want), put a snippet of php which connects to the mysql database, selects the 'hits' table and checks if theres a row which has the user's IP, if it returns 0 rows, add a new row with the user's IP in the 'IP' field, don't set the ID manually.


To see how many unique hits you've had, count your rows.



if you're lazy like me... Google Analytics | Official Website
Why count just let it output the highest ID number?
 
Why count just let it output the highest ID number?

because its simpler for a noob to go


PHP:
$sql = "SELECT * From Hits";
$query = mysql_query($sql);

$value = mysql_num_rows($query);

than

PHP:
$sql = "SELECT TOP 1 From Hits ORDER by ID desc";
$query = mysql_query($sql);

because he presumably does not know SQL.




-------

I know that I have not used best practices here, I'm just attempting to make my example easy to understand for people unfamiliar with PHP/SQL.
 
You could just use a file to keep the count, if you don't want to install/configure a database just for a counter....

PHP:
<?php

class Counter
{
	public static	
		$debug = array(),
		$data = array(),
		$count = 0;
	
	private static
		$file,
		$ip;
	
	
	public static function count($ip = False, $file = './counter.txt')
	{
		if(self::$count > 0 && !$ip && self::$file == $file)
		{
			self::$debug[] = 'using $count value from Last Call.';
			return self::$count;
		}
		
		//Assign Basic Info for Constructuction
		self::$file = $file;
		self::$ip = ( !$ip ) ? $_SERVER['REMOTE_ADDR'] : $ip;
		
		//Load Data
		self::$data = self::load();
		
		//Update Data (if needed)
		self::$data = self::update();
		
		//Create a variable for the current count
		self::$count = count(self::$data);
		return self::$count;
	}
	
	
	private static function update()
	{
		//Cycle through data and search for current IP:
		foreach(self::$data as $visitor_num => $ip)
		{
			if(self::$ip == $ip)
			{
				self::$debug[] = 'Found IP Match ('. self::$ip .')';
				//IP found, don't log.
				return self::$data;
			}
		}
		
		self::$debug[] = 'Writing IP to File ('. self::$ip .', '. self::$file .')';
		
		//Logging IP
		fwrite(fopen(self::$file, 'a'), self::$ip . "\n");
		
		//Return list of all IPs
		return self::load();
	}
	
	
	private static function load()
	{
		if(!file_exists(self::$file))
		{
			self::$debug[] = 'Creating File with IP ('. self::$file .', '. self::$ip .')';
			
			//File does not exist yet, create it with the current IP
			fwrite(fopen(self::$file, 'w'), self::$ip . "\n");
		}
		
		self::$debug[] = 'Reading File ('. self::$file .')';
		
		//List of all IPs
		return file(self::$file, FILE_IGNORE_NEW_LINES);
		
	}
}

To use this, simply use autoloading (or require the file with this class) and use the stuff below to access/work with count data:

PHP:
//Show Information
	echo '<p style="margin:0">Current Count: ' . Counter::count() . '</p>';
	echo '<pre>';

	echo "\tData:\n";
	print_r(Counter::$data);

	echo "\tDebug:\n";
	print_r(Counter::$debug);

	echo '</pre>';

Don't worry about calling the static method twice, it only runs the bulk of the script when it needs to. If run a second (or more) time(s), the static method returns the count. You cannot count the same IP (char-for-char) twice, so there's little reason to run the bulk of the script more than once per page-load. You can use mutliple count files, mix and match them, etc.

There are some reasons, though. You might want to log a list of IPs manually, or perhaps use a few different count files for different needs. (Note: The variable $ip doesn't even need to be an IP..)

Here's a script that creates a list of local IPs and adds it to the main count file as well as it's own local_ip_count file.
PHP:
//Add to list
	$local_ip_list = array('localhost', '127.0.0.1');

	for($i = 100; $i < 200; $i++)
	{
		$local_ip_list[] = '192.168.1.' . $i;
	}

	foreach($local_ip_list as $ip)
	{
		Counter::count($ip, 'counter.txt');
		
		//Backup
		Counter::count($ip, 'local_ip_count.txt');
	}

It adds each local IP both to the main count file, and a backup file.

Notice: If you use Counter::count() after you call Counter::count($ip, 'txt_file.txt'), the latest defined IP and text file will be used.

To Fix: You must manually assign the default values to reference them after you override them.
 
Last edited:
Back