<?php
/***************************************************************************
*
* Author : Eric Sizemore ( www.secondversion.com )
* Package : SV's Simple Counter
* Version : 1.5
* Copyright: (C) 2005-2006 Eric Sizemore
* Site : www.secondversion.com
* Email : esizemore05@gmail.com
* File : counter.php
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
***************************************************************************/
// #################### 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', false);
// Show count as images?
define('USE_IMAGES', true);
// Path to the images
define('IMG_DIR', 'counter/images/');
// Image extension
define('IMG_EXT', '.gif');
// ############################ Functions #############################
/*
This function will check if a given file exists and is writeable.
@param string Filename
@return boolean
*/
function check_exists_writable($file)
{
// Does the file exist, and is it writeable?
if (file_exists($file) AND is_writable($file))
{
return true;
}
else
{
return false;
}
}
/*
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
@param boolean Use file locking? Determined by what we set in define('USE_FLOCK')
@return mixed
*/
function fp($file, $mode, $data, $use_flock)
{
$fp = @fopen($file, $mode);
// Could the file be opened?
if (!$fp)
{
die('Error: ' . $file . ' could not be opened.');
}
// Use file locking?
if ($use_flock)
{
if (@flock($fp, LOCK_EX))
{
if ($mode == 'r')
{
return @fread($fp, filesize($file));
}
else
{
@fwrite($fp, $data);
}
@flock($fp, LOCK_UN);
}
else
{
// Looks as if we couldn't lock the file
// prompt the user to set USE_FLOCK to false.
die('Could not lock the file ' . $file . '.<br>Set USE_FLOCK to false.');
}
}
else
{
// No, we're not using file locking
if ($mode == 'r')
{
return @fread($fp, filesize($file));
}
@fwrite($fp, $data);
}
@fclose($fp);
}
// ######################## Start Main Script #########################
if (!check_exists_writable(COUNT_FILE))
{
die('Error: ' . COUNT_FILE . ' does not exist or is not writeable.');
}
else if (!check_exists_writable(IP_FILE))
{
die('Error: ' . IP_FILE . ' does not exist or is not writeable.');
}
else
{
// Get current count
$count = fp(COUNT_FILE, 'r', '', USE_FLOCK);
// Do we only want to count 'unique' visitors?
if (ONLY_UNIQUE)
{
// Get visitor ip and check against our ip log
$ip = (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
$ips = fp(IP_FILE, 'r', '', USE_FLOCK);
$ips = explode("\n", $ips);
$visited = (in_array($ip, $ips)) ? true : false;
// They've not visited before
if (!$visited)
{
fp(IP_FILE, 'a', $ip . "\n", USE_FLOCK);
fp(COUNT_FILE, 'w', $count + 1, USE_FLOCK);
}
// 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)
{
$len = strlen($count);
$display = '';
for($i = 0; $i < $len; $i++)
{
$count = substr($count, $i, 1);
$display .= '<a href="http://www.secondversion.com" title="Simple PHP counter by SecondVersion.com"><img src="' . IMG_DI
R . $count . IMG_EXT . '" border="0" alt="Simple PHP counter by SecondVersion.com"><img src="' . IMG_DIR . 'spacer' . IMG_EXT . '" b
order="0" alt=""></a>';
}
echo $display;
}
else
{
// Nope, let's just show it as plain text
echo '<a href="http://www.secondversion.com" title="Simple PHP counter by SecondVersion.com">' . $count . '</a>';
}
}
// EoF
?>