tidy up code in counter

This commit is contained in:
Ben Harris 2020-02-04 00:28:51 -05:00
parent b5a9ec9998
commit fc203d55bd
2 changed files with 157 additions and 160 deletions

View File

@ -1,159 +1,156 @@
<?php <?php
/******************************************************************************* /*******************************************************************************
* Title: PHP graphical hit counter (GCount) * Title: PHP graphical hit counter (GCount)
* Version: 1.3.1 from 4th January 2015 * Version: 1.3.1 from 4th January 2015
* Author: Klemen Stirn * Author: Klemen Stirn
* Website: http://www.phpjunkyard.com * Website: http://www.phpjunkyard.com
******************************************************************************** ********************************************************************************
* COPYRIGHT NOTICE * COPYRIGHT NOTICE
* Copyright 2004-2015 Klemen Stirn. All Rights Reserved. * Copyright 2004-2015 Klemen Stirn. All Rights Reserved.
* This script may be used and modified free of charge by anyone * This script may be used and modified free of charge by anyone
* AS LONG AS COPYRIGHT NOTICES AND ALL THE COMMENTS REMAIN INTACT. * AS LONG AS COPYRIGHT NOTICES AND ALL THE COMMENTS REMAIN INTACT.
* By using this code you agree to indemnify Klemen Stirn from any * By using this code you agree to indemnify Klemen Stirn from any
* liability that might arise from it's use. * liability that might arise from it's use.
* Selling the code for this program, in part or full, without prior * Selling the code for this program, in part or full, without prior
* written consent is expressly forbidden. * written consent is expressly forbidden.
* Using this code, in part or full, to create derivate work, * Using this code, in part or full, to create derivate work,
* new scripts or products is expressly forbidden. Obtain permission * new scripts or products is expressly forbidden. Obtain permission
* before redistributing this software over the Internet or in * before redistributing this software over the Internet or in
* any other medium. In all cases copyright and header must remain intact. * any other medium. In all cases copyright and header must remain intact.
* This Copyright is in full effect in any country that has International * This Copyright is in full effect in any country that has International
* Trade Agreements with the United States of America or * Trade Agreements with the United States of America or
* with the European Union. * with the European Union.
******************************************************************************** ********************************************************************************
ACKNOWLEDGEMENT ACKNOWLEDGEMENT
Please support future script development by linking to us: Please support future script development by linking to us:
http://www.phpjunkyard.com/about/link2us.php http://www.phpjunkyard.com/about/link2us.php
Or by sending a small donation: Or by sending a small donation:
http://www.phpjunkyard.com/about/donate.php http://www.phpjunkyard.com/about/donate.php
*******************************************************************************/ *******************************************************************************/
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// SETTINGS // SETTINGS
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// URL of the folder where script is installed. INCLUDE a "/" at the end! // URL of the folder where script is installed. INCLUDE a "/" at the end!
$base_url = 'counter/'; $base_url = 'counter/';
// Default image style (font) // Default image style (font)
$default_style = 'cntdwn'; $default_style = 'cntdwn';
// Default counter image extension // Default counter image extension
$default_ext = 'gif'; $default_ext = 'gif';
// Count unique visitors? 1 = YES, 0 = NO // Count unique visitors? 1 = YES, 0 = NO
$count_unique = 0; $count_unique = 0;
// Number of hours a visitor is considered as "unique" // Number of hours a visitor is considered as "unique"
$unique_hours = 24; $unique_hours = 24;
// Minimum number of digits shown (zero-padding). Set to 0 to disable. // Minimum number of digits shown (zero-padding). Set to 0 to disable.
$min_digits = 0; $min_digits = 0;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// DO NOT EDIT BELOW // DO NOT EDIT BELOW
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Turn error notices off // Turn error notices off
error_reporting(E_ALL ^ E_NOTICE); error_reporting(E_ALL ^ E_NOTICE);
// Set the correct MIME type // Set the correct MIME type
header("Content-type: text/javascript"); header("Content-type: text/javascript");
// Tell browsers not to cache the file output so we can count all hits // Tell browsers not to cache the file output so we can count all hits
header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false); header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); header("Pragma: no-cache");
// Is page ID set? // Is page ID set?
if ( ! isset($_GET['page']) ) if (!isset($_GET['page'])) {
{ die('ERROR: The gcount.php file must be called with a <b>?page=PAGEID</b> parameter, for example <b>gcount.php?page=test</b>');
die('ERROR: The gcount.php file must be called with a <b>?page=PAGEID</b> parameter, for example <b>gcount.php?page=test</b>'); }
}
// Remove any illegal chars from the page ID
// Remove any illegal chars from the page ID $page = preg_replace('/[^a-zA-Z0-9\-_\.]/', '', $_GET['page']);
$page = preg_replace('/[^a-zA-Z0-9\-_\.]/','',$_GET['page']);
// Stop if $page is not valid
// Stop if $page is not valid if (!strlen($page)) {
if ( ! strlen($page) ) die('ERROR: Page ID is missing or contains only invalid chars. Please use only these chars for the page ID: a-z, A-Z, 0-9, &quot;.&quot;, &quot;-&quot; and &quot;_&quot;');
{ }
die('ERROR: Page ID is missing or contains only invalid chars. Please use only these chars for the page ID: a-z, A-Z, 0-9, &quot;.&quot;, &quot;-&quot; and &quot;_&quot;');
} // Get style and extension information
$style = isset($_GET['style'])
// Get style and extension information ? preg_replace('/[^a-zA-Z0-9\-_\.]/', '', $_GET['style'])
$style = isset($_GET['style']) ? preg_replace('/[^a-zA-Z0-9\-_\.]/','',$_GET['style']) : $default_style; : $default_style;
$ext = isset($_GET['ext']) ? preg_replace('/[^a-zA-Z0-9\-_\.]/','',$_GET['ext']) : $default_ext;
$ext = isset($_GET['ext'])
// Set values for cookie, log file and style flye names ? preg_replace('/[^a-zA-Z0-9\-_\.]/', '', $_GET['ext'])
$cname = 'gcount_unique_'.$page; : $default_ext;
$logfile = 'logs/' . $page . '.txt';
$style_dir = 'styles/' . $style . '/'; // Set values for cookie, log file and style flye names
$cname = 'gcount_unique_'.$page;
// Does the log file exist? $logfile = 'logs/' . $page . '.txt';
if ( ! file_exists($logfile) ) $style_dir = 'styles/' . $style . '/';
{
die('ERROR: Log file not found. Make sure there is a file called <b>' . $page . '.txt</b> inside your <b>logs</b> folder. On most servers file names are CaSe SeNSiTiVe!'); // Does the log file exist?
} if (!file_exists($logfile)) {
die('ERROR: Log file not found. Make sure there is a file called <b>' . $page . '.txt</b> inside your <b>logs</b> folder. On most servers file names are CaSe SeNSiTiVe!');
// Open log file for reading and writing }
if ($fp = @fopen($logfile, 'r+'))
{ // Open log file for reading and writing
// Lock log file from other scripts if ($fp = @fopen($logfile, 'r+')) {
$locked = flock($fp, LOCK_EX); // Lock log file from other scripts
$locked = flock($fp, LOCK_EX);
// Lock successful?
if ($locked) // Lock successful?
{ if ($locked) {
// Let's read current count // Let's read current count
$count = intval( trim( fread($fp, filesize($logfile) ) ) ); $count = intval(trim(fread($fp, filesize($logfile))));
// If counting unique hits is enabled make sure it's a unique hit // If counting unique hits is enabled make sure it's a unique hit
if ( $count_unique == 0 || ! isset($_COOKIE[$cname]) ) if ($count_unique == 0 || ! isset($_COOKIE[$cname])) {
{ // Update count by 1 and write the new value to the log file
// Update count by 1 and write the new value to the log file $count = $count + 1;
$count = $count + 1; rewind($fp);
rewind($fp); fwrite($fp, $count);
fwrite($fp, $count);
// Print the Cookie and P3P compact privacy policy
// Print the Cookie and P3P compact privacy policy header('P3P: CP="NOI NID"');
header('P3P: CP="NOI NID"'); setcookie($cname, 1, time()+60*60*$unique_hours);
setcookie($cname, 1, time()+60*60*$unique_hours); }
} }
} else {
else // Lock not successful. Better to ignore than to damage the log file
{ $count = 1;
// Lock not successful. Better to ignore than to damage the log file }
$count = 1;
} // Release file lock and close file handle
flock($fp, LOCK_UN);
// Release file lock and close file handle fclose($fp);
flock($fp, LOCK_UN); }
fclose($fp); else {
} die("ERROR: Can't read/write to the log file ($logfile). Make sure this file is writable by PHP scripts. On UNIX servers, CHMOD the log file to 666 (rw-rw-rw-).");
else }
{
die("ERROR: Can't read/write to the log file ($logfile). Make sure this file is writable by PHP scripts. On UNIX servers, CHMOD the log file to 666 (rw-rw-rw-)."); // Is zero-padding enabled? If yes, add zeros if required
} if ($min_digits) {
$count = sprintf('%0' . $min_digits . 's', $count);
// Is zero-padding enabled? If yes, add zeros if required }
if ($min_digits)
{ // Print out Javascript code and exit
$count = sprintf('%0'.$min_digits.'s', $count); $len = strlen($count);
} for ($i = 0; $i < $len; $i++) {
$url = $base_url . $style_dir . substr($count, $i, 1) . '.' . $ext;
// Print out Javascript code and exit echo 'document.write(\'<img src="'. $url .'" border="0" />\');';
$len = strlen($count); }
for ($i=0; $i<$len; $i++)
{ exit();
echo 'document.write(\'<img src="'.$base_url . $style_dir . substr($count,$i,1) . '.' . $ext .'" border="0" />\');'; ?>
}
exit();
?>

View File

@ -1 +1 @@
303 333