PHPBuilder - PHP Text Hit Counter



RSS Twitter
Snippets Other

PHP Text Hit Counter

by: Rick Christy
|
March 12, 2003

Version: 0.2

Type: Full Script

Category: Other

License: GNU General Public License

Description: This script will create a new COUNTER FILE in the CONFIGURED PATH named as the full path to the CALLING SCRIPT with slashes replaced by underscores and the name of the calling script appended to the end of it, as reported by PHP via the $_SERVER['PHP_SELF'] variable.



<?php
/*=============================================================================
*
* Text Hit Counter v0.2
* By: Richard James Christy (grymmjack@sector7bbs.com)
* http://www.sector7bbs.com/grymmjack
*
* NOTICE: THIS SCRIPT REQUIRES PHP VERSION 4 OR HIGHER
*
* MODULE: hit_counter.php
* 						MODIFIED: 3/12/2003 8:16PM
*
*==============================================================================


NOTE: To properly view this source code you will need a machine capable of 80
      column text display. Optimally, disable WORD WRAPPING in your viewer or
      editor.
      
      
DESCRIPTION
-----------

This script will create a new COUNTER FILE in the CONFIGURED PATH named as the
full path to the CALLING SCRIPT with slashes replaced by underscores and
the name of the calling script appended to the end of it, as reported by PHP
via the $_SERVER['PHP_SELF'] variable.

Ex: Calling Script = /mypage/mysection/index.php
    Counter File   = _mypage_mysection_index.php

In this way, it is easy for you to know which counter goes with which script,
and you can then manipulate or delete them at your discretion.

The contents of this file track the date in which the COUNTER FILE was first
created, and the number of hits to the page.

The data gathered from this COUNTER FILE is returned in the form of an
associative array as follows:

$hit_stats['since']    : When the counter was first initialized.
$hit_stats['hits']     : How many hits this counter has counted since init.
$hit_stats['visitors'] : A smart description of visitors (if there has only
                         been 1 visitor, it will say 1 visitor instead of
                         1 visitors).


INSTALLATION
------------

1. Create a new directory and CHMOD(777) it. It needs to be world readable,
   and world writeable.

2. Configure the script below. Do not change anything beyond the line warning
   you that if you do it may break the code.

3. From each script where you wish to use this counter, place the following
   code BEFORE you make any reference to your stats.

   <?php
        require_once('/path/to/script/hit_counter.php');
        $hit_stats = hit_counter();
   ?>

4. Wherever you want to access your statistics, use code such as this:

   <?php
        print "$hit_stats[hits] $hit_stats[visitors] since $hit_stats[since]";
   ?>


CONFIGURATION
-------------

$_COUNTER_PATH_ : The fully qualified file system path to the directory you
                  created (if you followed instructions) in installation step
                  number 1. INCLUDE TRAILING SLASH!

$_DATE_FORMAT_  : The PHP date() format specifier you wish to use for your
                  COUNTER FILE initialization timestamp.

NOTE: The below text was taken from the PHP Manual and included here for
      the sake of convenience...

The following characters are recognized in the format string: 

a - "am" or "pm" 
A - "AM" or "PM" 
B - Swatch Internet time 
d - day of the month, 2 digits with leading zeros; i.e. "01" to "31" 
D - day of the week, textual, 3 letters; e.g. "Fri" 
F - month, textual, long; e.g. "January" 
g - hour, 12-hour format without leading zeros; i.e. "1" to "12" 
G - hour, 24-hour format without leading zeros; i.e. "0" to "23" 
h - hour, 12-hour format; i.e. "01" to "12" 
H - hour, 24-hour format; i.e. "00" to "23" 
i - minutes; i.e. "00" to "59" 
I (capital i) - "1" if Daylight Savings Time, "0" otherwise. 
j - day of the month without leading zeros; i.e. "1" to "31" 
l (lowercase 'L') - day of the week, textual, long; e.g. "Friday" 
L - boolean for whether it is a leap year; i.e. "0" or "1" 
m - month; i.e. "01" to "12" 
M - month, textual, 3 letters; e.g. "Jan" 
n - month without leading zeros; i.e. "1" to "12" 
O - Difference to Greenwich time in hours; e.g. "+0200" 
r - RFC 822 formatted date; e.g. "Thu, 21 Dec 2000 16:01:07 +0200" (added in PHP 4.0.4) 
s - seconds; i.e. "00" to "59" 
S - English ordinal suffix for the day of the month, 2 characters; i.e. "st", "nd", "rd" or "th" 
t - number of days in the given month; i.e. "28" to "31" 
T - Timezone setting of this machine; e.g. "EST" or "MDT" 
U - seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) 
w - day of the week, numeric, i.e. "0" (Sunday) to "6" (Saturday) 
W - ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0) 
Y - year, 4 digits; e.g. "1999" 
y - year, 2 digits; e.g. "99" 
z - day of the year; i.e. "0" to "365" 
Z - timezone offset in seconds (i.e. "-43200" to "43200"). The offset for timezones west of UTC is always negative, and for those east of UTC is always positive. 
Unrecognized characters in the format string will be printed as-is. 

$_SINGULAR_HIT_ : The string description of the $hit_stats[visitors] variable
                  that is returned when there is currently only 1 hit.

$_MULTIPLE_HIT_ : The string description of the $hit_stats[visitors] variable
                  that is returned when there is more than 1 hit.

*----------------------------------------------------------------------------*/



// CONFIGURE THE SCRIPT BELOW...

$_COUNTER_PATH_ = $_SERVER['DOCUMENT_ROOT'] . "counter.data/";
$_DATE_FORMAT_  = "m/d/Y";
$_SINGULAR_HIT_ = "visitor";
$_MULTIPLE_HIT_ = "visitors";



/******************************************************************************
DO NOT CHANGE ANYTHING BEYOND THIS LINE IF YOU DO NOT WANT TO BREAK ANYTHING!!!
******************************************************************************/

function hit_counter() {
	global $_COUNTER_PATH_;
	global $_DATE_FORMAT_;
	global $_SINGULAR_HIT_;
	global $_MULTIPLE_HIT_;
	$counterFile = $_COUNTER_PATH_ . str_replace('/', '_', $_SERVER['PHP_SELF']);
	if (!file_exists($counterFile)) {
		$fp = fopen($counterFile, 'w');
		$since = date($_DATE_FORMAT_);
		fputs($fp, $since . ",0");
		fclose($fp);
	}
	$fp = fopen($counterFile, 'r+');
	$data = fgetcsv($fp, 1024);
	$since = $data[0];
	$num = $data[1];
	$num += 1;	
	rewind($fp);
	fputs($fp, $since . "," . $num);
	fclose($fp);
	$stats['hits'] = number_format($num);
	($num == 1) ? $stats['visitors'] = $_SINGULAR_HIT_ : $stats['visitors'] = $_MULTIPLE_HIT_;
	$stats['since'] = $since;
	return $stats;
}

?>

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Rick Christy

Comment:



Comment:

(Maximum characters: 1200). You have characters left.