<?php /* Simple TextCounter Using DBM, v1.0
* textcounter.php3
*
* Author - Christopher Sean Morrison
* E-mail - morrison@jhu.edu
*
* Description - This is just your basic text counter. It uses
* dbm for storing and retrieving its page hit counts. This
* counter does not limit the page counts by IP or anything of
* the sort, it just increments the counter on a page every time
* it is loaded. You can set up a hit count on a page-by-page
* basis, or have a single counter for the entire site.
*
* Installation and Usage - To install, just add this file as
* textcounter.php3 where you keep any other php class library
* files (like ~/public_html/libray). Then set the value for
* $DBFILE (below) in the class definition to the location of
* of the counter dbm file. World read-write will need to be
* given to the file in most cases so make sure the file is in
* a save place (like outside the web dir). See the examples
* below for more information on initializing a new database
* and adding the counter to pages.
*
* Example use #1 - To Create a New Counter
* Create a dummy web page (or use your favorite dbm app) and
* have the counter(s) that you want set, initialized. Below, we
* create a counter object, create the database, and then set
* the counter value for "filename.php3" to 1000. Note that
* create will only need to be run *once* to set up the dbm file.
*
* <?php
* include("library/textcounter.php3");
* $counter = new TextCounter;
* $counter->create();
* $counter->set("filename.php3", 1000);
* ?>
*
* Example use #2 - To Add a Counter for a Single Page
* Adding a counter to one of your pages is very simple. First,
* we capture the name of the file were in (which you have already
* have set in a variable); be careful with nested files. Then,
* we just increment the counter and display it.
*
* <?php
* include ("library/textcounter.php3");
* $filename = basename(__FILE__);
* $counter = new TextCounter;
* $counter->update($filename);
* echo $counter->value($filename);
* ?>
*
* Example use #3 - To Reset all Counters on a Website
* To reset all of the counters on a site, just call the reset()
* method passing the value you want all counters to be set to.
* Note that if the page entries don't already exist (in the case
* where every page tracks it's own hit count) they can't be
* reset.
*
* <?php
* include ("library/textcounter.php3");
* $counter = new TextCounter;
* $counter->reset(0);
* ?>
*/
Class TextCounter {
// Leave $COUNT set to 0
var $COUNT = 0;
// Set $DBFILE to the location where you would like the dbm file
// to be kept. For security reasons, make sure it is at least
// outside the web dir. The file will need world read-write
// permissions ('chmod 707 /home/christi/counter')
var $DBFILE = "/home/christi/counter";
// create() creates a new database if one is not existing. An
// empty file will need to be there first (ftp one over) if you
// do not have direct/shell access (i.e. you need permission to
// create a file).
function create() {
$dbm = dbmopen($this->DBFILE, "c");
dbmclose($dbm);
}
// exists() tests for the existance of the database, as well as
// proper permission to write to the file. This could be useful
// to set up a counter management page. (not fully tested)
function exists() {
if (!file_exists($this->DBFILE)) {
return 0;
}
if (!is_writeable($this->DBFILE)) {
return 0;
}
return 1;
}
// set() sets a single counter to some numeric value. Normally,
// you will probably want the base name of the file being counted
// to be the $key, and $value as the value for that file's
// counter. Careful with this one since you can wipe out a
// counter's value.
function set($key="unspecified", $value=0) {
$dbm = dbopen($this->DBFILE, "w");
dbmreplace($dbm, $key, $value);
dbmclose($dbm);
}
// update() increments the counter for a given page ($key).
function update($key="unspecified") {
$dbm = dbmopen($this->DBFILE, "w");
if (dbmexists($dbm, $key)) {
$this->COUNT = dbmfetch($dbm, $key);
}
dbmreplace($dbm, $key, ++$this->COUNT);
dbmclose($dbm);
}
// value() retrieves the value for a counter. Used with php's
// echo or print call, you have a counter value output to your
// page.
function value($key="unspecified") {
if ($this->COUNT > 0) {
$dbm = dbmopen($this->DBFILE, "r");
if (dbmexists($dbm, $key)) {
$this->COUNT = dbmfetch($dbm, $key);
}
dbmclose($dbm);
}
return $this->COUNT;
}
// reset() resets ALL the values of all of the counters that have a
// record (all key's in the dbm file) in the database. BE CAREFULL
// with reset since it resets all values to $value. Uncomment the
// echo line to find out how many keys were reset.
function reset($value=0) {
$keycount = 0;
$dbm = dbmopen($this->DBFILE, "w");
$key = dbmfirstkey($dbm);
while ($key) {
$keycount++;
dbmreplace($dbm, $key, $value);
$key = dbmnextkey($dbm, $key);
}
//echo "[" . $keycount . "] keys were found and their values were set to: [" . $value . "]";
}
}
?>
|
|