PDA

View Full Version : Unique hits



Ini
03-06-2007, 04:38 PM
Looking for the code to show unique hits not page hits.

Thanks +rep for help

Mentor
03-06-2007, 04:43 PM
unque hits is harder as it needs to keep track of who its already counted, hence the script has to log vistors and check against the records to know weather to add to the total count. Useuallly this will be refreshed every 24 hours or simlar time fraim and hits will be counted again, so as to save room on the logs.

Easyest method is use a proper states collecting app, tracewatch for example, although i suppose a script wouldnt be to hard to create for it. Im sure google will probabaly have a few ready to use anyway.

Ini
03-06-2007, 04:45 PM
unque hits is harder as it needs to keep track of who its already counted, hence the script has to log vistors and check against the records to know weather to add to the total count. Useuallly this will be refreshed every 24 hours or simlar time fraim and hits will be counted again, so as to save room on the logs.

Easyest method is use a proper states collecting app, tracewatch for example, although i suppose a script wouldnt be to hard to create for it. Im sure google will probabaly have a few ready to use anyway.

Thanks ill have a look for one now.

+rep

Luckyrare
03-06-2007, 05:26 PM
You can do a number of things to log "unique" views.

You could set a session/cookie and expire after 24 hours.

OR

You can log each IP, if that IP isnt in the log (last 24 hours) it adds it otherwise does nothing. This would be a CPU intensive.

Joltersoft
03-06-2007, 05:27 PM
google maybe?

Ini
03-06-2007, 05:37 PM
google maybe?

Way to point out the obvious :P

Florx
03-06-2007, 06:54 PM
Heres one I found on the interweb :D

It uses 2 text files which need to be chmoded to 777 to let it work properly. Alternativly you could use AWstats which show unique hits :O:D


<?php

// #################### Define Important Constants ####################
// There should be no need to edit these
define('COUNT_FILE', 'counter.txt');
define('IP_FILE', 'ips.txt');

// ######################## USER CONFIGURATION ########################
// Edit the following.. true = yes, false = no

// Use file locking?
define('USE_FLOCK', true);

// Count only unique visitors?
define('ONLY_UNIQUE', true);

// Show count as images?
define('USE_IMAGES', false);

// Path to the images
define('IMG_DIR', 'counter/images/');

// Image extension
define('IMG_EXT', '.gif');

// ############################ Functions #############################
/**
* We use this function to open, read/write to files.
*
* @param string Filename
* @param string Mode (r, w, a, etc..)
* @param string If writing to the file, the data to write
* @return mixed
*/
function fp($file, $mode, $data = '')
{
if (!file_exists($file) OR !is_writable($file))
{
trigger_error("Error: '<code>$file</code>' does not exist or is not writable.");
}

if (!($fp = @fopen($file, $mode)))
{
trigger_error("Error: '<code>$file</code>' could not be opened.");
}

if (USE_FLOCK AND @flock($fp, LOCK_EX))
{
if ($mode == 'r')
{
return @fread($fp, filesize($file));
}
else
{
@fwrite($fp, $data);
}
@flock($fp, LOCK_UN);
}
else
{
if ($mode == 'r')
{
return @fread($fp, filesize($file));
}
@fwrite($fp, $data);
}
@fclose($fp);
}

/**
* Get the users ip address.
* Pulled from my {@link http://www.domainportfolio.us Domain Portfolio} project.
*
* @param none
* @return string
*/
function get_ip()
{
if ($_SERVER['HTTP_X_FORWARDED_FOR'])
{
if (preg_match_all("#[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}#s", $_SERVER['HTTP_X_FORWARDED_FOR'], $ips))
{
while (list($key, $val) = @each($ips[0]))
{
if (!preg_match("#^(10|172\.16|192\.168)\.#", $val))
{
$ip = $val;
break;
}
}
}
}
else if ($_SERVER['HTTP_CLIENT_IP'])
{
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
else if ($_SERVER['HTTP_FROM'])
{
$ip = $_SERVER['HTTP_FROM'];
}
else
{
$ip = $_SERVER['REMOTE_ADDR'];
}
return preg_replace('#([^0-9,\.]+)#', '', $ip);
}

// ######################## Start Main Script #########################
// Get current count
$count = fp(COUNT_FILE, 'r');

// Do we only want to count 'unique' visitors?
if (ONLY_UNIQUE)
{
// Get visitor ip and check against our ip log
$ip = get_ip();
$ips = trim(fp(IP_FILE, 'r'));
$ips = preg_split("#\n#", $ips, -1, PREG_SPLIT_NO_EMPTY);
$visited = (bool)(in_array($ip, $ips));

// They've not visited before
if (!$visited)
{
fp(IP_FILE, 'a', "$ip\n");
fp(COUNT_FILE, 'w', $count + 1);
}
// Memory saving
unset($ips);
}
else
{
// No, we wish to count all visitors
fp(COUNT_FILE, 'w', $count + 1, USE_FLOCK);
}

// Do we want to display the # visitors as graphics?
if (USE_IMAGES)
{
$count = preg_split("##", $count, -1, PREG_SPLIT_NO_EMPTY);
$len = count($count);
$display = '';

for ($i = 0; $i < $len; $i++)
{
$display .= '<img src="' . IMG_DIR . $count[$i] . IMG_EXT . '" border="0" alt="Simple PHP counter by SecondVersion.com" />&nbsp;';
}
echo "<a href=\"http://www.secondversion.com\" style=\"text-decoration: none;\" title=\"Simple PHP counter by SecondVersion.com\">$display</a>";
}
else
{
// Nope, let's just show it as plain text
echo "$count";
}
// EoF

?>

Ini
03-06-2007, 07:02 PM
Heres one I found on the interweb :D

It uses 2 text files which need to be chmoded to 777 to let it work properly. Alternativly you could use AWstats which show unique hits :O:D


<?php

// #################### Define Important Constants ####################
// There should be no need to edit these
define('COUNT_FILE', 'counter.txt');
define('IP_FILE', 'ips.txt');

// ######################## USER CONFIGURATION ########################
// Edit the following.. true = yes, false = no

// Use file locking?
define('USE_FLOCK', true);

// Count only unique visitors?
define('ONLY_UNIQUE', true);

// Show count as images?
define('USE_IMAGES', false);

// Path to the images
define('IMG_DIR', 'counter/images/');

// Image extension
define('IMG_EXT', '.gif');

// ############################ Functions #############################
/**
* We use this function to open, read/write to files.
*
* @param string Filename
* @param string Mode (r, w, a, etc..)
* @param string If writing to the file, the data to write
* @return mixed
*/
function fp($file, $mode, $data = '')
{
if (!file_exists($file) OR !is_writable($file))
{
trigger_error("Error: '<code>$file</code>' does not exist or is not writable.");
}

if (!($fp = @fopen($file, $mode)))
{
trigger_error("Error: '<code>$file</code>' could not be opened.");
}

if (USE_FLOCK AND @flock($fp, LOCK_EX))
{
if ($mode == 'r')
{
return @fread($fp, filesize($file));
}
else
{
@fwrite($fp, $data);
}
@flock($fp, LOCK_UN);
}
else
{
if ($mode == 'r')
{
return @fread($fp, filesize($file));
}
@fwrite($fp, $data);
}
@fclose($fp);
}

/**
* Get the users ip address.
* Pulled from my {@link http://www.domainportfolio.us Domain Portfolio} project.
*
* @param none
* @return string
*/
function get_ip()
{
if ($_SERVER['HTTP_X_FORWARDED_FOR'])
{
if (preg_match_all("#[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}#s", $_SERVER['HTTP_X_FORWARDED_FOR'], $ips))
{
while (list($key, $val) = @each($ips[0]))
{
if (!preg_match("#^(10|172\.16|192\.168)\.#", $val))
{
$ip = $val;
break;
}
}
}
}
else if ($_SERVER['HTTP_CLIENT_IP'])
{
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
else if ($_SERVER['HTTP_FROM'])
{
$ip = $_SERVER['HTTP_FROM'];
}
else
{
$ip = $_SERVER['REMOTE_ADDR'];
}
return preg_replace('#([^0-9,\.]+)#', '', $ip);
}

// ######################## Start Main Script #########################
// Get current count
$count = fp(COUNT_FILE, 'r');

// Do we only want to count 'unique' visitors?
if (ONLY_UNIQUE)
{
// Get visitor ip and check against our ip log
$ip = get_ip();
$ips = trim(fp(IP_FILE, 'r'));
$ips = preg_split("#\n#", $ips, -1, PREG_SPLIT_NO_EMPTY);
$visited = (bool)(in_array($ip, $ips));

// They've not visited before
if (!$visited)
{
fp(IP_FILE, 'a', "$ip\n");
fp(COUNT_FILE, 'w', $count + 1);
}
// Memory saving
unset($ips);
}
else
{
// No, we wish to count all visitors
fp(COUNT_FILE, 'w', $count + 1, USE_FLOCK);
}

// Do we want to display the # visitors as graphics?
if (USE_IMAGES)
{
$count = preg_split("##", $count, -1, PREG_SPLIT_NO_EMPTY);
$len = count($count);
$display = '';

for ($i = 0; $i < $len; $i++)
{
$display .= '<img src="' . IMG_DIR . $count[$i] . IMG_EXT . '" border="0" alt="Simple PHP counter by SecondVersion.com" />&nbsp;';
}
echo "<a href=\"http://www.secondversion.com\" style=\"text-decoration: none;\" title=\"Simple PHP counter by SecondVersion.com\">$display</a>";
}
else
{
// Nope, let's just show it as plain text
echo "$count";
}
// EoF

?>

Tyvm +rep

Foobar
03-06-2007, 07:19 PM
Way to point out the obvious :PIf it was so obvious why didn't you use it?

Ini
03-06-2007, 07:23 PM
If it was so obvious why didn't you use it?

I did ;l

Want to hide these adverts? Register an account for free!