Simple, Easy PHP Hit Counter





4
Date Submitted Wed. Nov. 7th, 2007 2:45 PM
Revision 1 of 1
Helper Idlemind
Tags PHP
Comments 2 comments
If it's a .php page, you can simply include this file where you want a hit counter to appear.

One file - text output. Very simple, very easy. Based off the filename of the page (creates a pagename.counter file to hold the count).
(No use for it myself - made it for a friend).

<?
//
// phpcounter.php
//
$myPage = $_SERVER['DOCUMENT_ROOT'];
$lastPeriod = strrpos($myPage,".");
$lastSlash = strrpos($myPage,"/") + 1;
if(!$lastSlash)
{
  $lastSlash=1;
}
$myLength = $lastPeriod-$lastSlash;
$newString = substr($myPage,$lastSlash,$myLength);
$newString = $newString . '.counter';
$existingfile = file_exists($newString);
if(!$existingfile)
{
  $hits[0] = 0;
  $fp = fopen($newString , "w");
  fputs($fp , "$hits[0]");
  fclose($fp);
}
$hits = file($newString);
$hits[0]++;
$fp = fopen($newString , "w");
fputs($fp , "$hits[0]");
fclose($fp);
echo $hits[0];
?>
 

Brian Stork

Comments

Comments Or you can use a Database
Thu. Apr. 10th, 2008 2:44 PM    Beginner stutimandal
Comments too simple
Mon. Oct. 13th, 2008 10:56 PM    Beginner jmardones

Voting