PHP Scripts for SVs Simple Counter

SVs Simple Counter, a simple PHP counter that counts your website visitors.SVs Simple Counter now has the ability to either show the count as plain text, or images; and whether or not to count only unique hits, or all hits.

 

 

<?php

/**
* @author    Eric Sizemore <admin@secondversion.com>
* @package   SV's Simple Counter
* @link      www.secondversion.com
* @version   1.7.0
* @copyright (C) 2006 - 2008 Eric Sizemore
* @license   {@see LICENSE.txt} GNU Public License
*/


// #################### Define Important Constants ####################
// There should be no need to edit these
define('COUNT_FILE', 'counter/logs/counter.txt');
define('IP_FILE', 'counter/logs/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>" . htmlspecialchars($file) . "</code>' does not exist or is not writable.");
 }

 if (!($fp = @fopen($file, $mode)))
 {
 trigger_error("Error: '<code>" . htmlspecialchars($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()
{
 $ip = my_getenv('REMOTE_ADDR');

 if (my_getenv('HTTP_X_FORWARDED_FOR'))
 {
 if (preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', my_getenv('HTTP_X_FORWARDED_FOR'), $matches))
 {
 foreach ($matches[0] AS $match)
 {
 if (!preg_match('#^(10|172\.16|192\.168)\.#', $match))
 {
 $ip = $match;
 break;
 }
 }
 unset($matches);
 }
 }
 else if (my_getenv('HTTP_CLIENT_IP'))
 {
 $ip = my_getenv('HTTP_CLIENT_IP');
 }
 else if (my_getenv('HTTP_FROM'))
 {
 $ip = my_getenv('HTTP_FROM');
 }
 return $ip;
}

/**
* Returns an environment variable. Based on PMA_getenv from phpMyAdmin.
*
* @param  string  Variable name, eg: PHP_SELF
* @return string
*/

function my_getenv($varname)
{
 if (isset($_SERVER[$varname]))
 {
 return $_SERVER[$varname];
 }
 else if (isset($_ENV[$varname]))
 {
 return $_ENV[$varname];
 }
 else if (getenv($varname))
 {
 return getenv($varname);
 }
 return '';
}

// ######################## 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="' . $count[$i] . '" />&amp;nbsp;';
 }
 echo $display;
}
else
{
 // Nope, let's just show it as plain text
 echo $count;
}

?>

&amp;nbsp;

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>