PX : code

Simple Counter by Vamsi Nath
Download this code


<?

/* PHP Counter script
   Written by Vamsi Nath
   Copyright 3/30/99
   php@vamsi.net

   Credit for getting the script
   to handle sub-directories properly
   goes to Boz@musheen.com
*/


function get_count($PHP_SELF) {

//Let's find the directory where
//the webpages are stored.
global $DOCUMENT_ROOT;

//Strip off file path and just get filename of the page the 
//counter appears on
$base_name = basename("$PHP_SELF");
$dir_name = dirname("$PHP_SELF");

//Define base 'counter' directory where the count files will be stored.
$count_file_root = "$DOCUMENT_ROOT" . "counters";
$count_file_base = "$base_name" . "_count";
$count_file = "$count_file_root" . "$dir_name/" . "$count_file_base";

//If counter directory doesn't exist, create it.
if(!is_dir("$count_file_root" . "$dir_name")){ mkdir("$count_file_root" .
"$dir_name", 0700); }


//Check to see if counter file exists
if (file_exists("$count_file")) {
    
    $file = file($count_file); #Store contents of counter file in array

    $split = split(",",$file[0], 2); #Retrieve file count
    $count = chop($split[1]); #Chop off whitespace
    ++$count;

    //Now let's write the updated count back to the counter file    
    $tf = @fopen("$count_file","w+");
    fwrite($tf,"$count_file,");
    fwrite($tf,"$count");
    @fclose($count_file);
    return $count;
}

else {
$tf = @fopen("$count_file","w+");

fwrite($tf,"$count_file,");
fwrite($tf,"1");
@fclose("$count_file");
$count = "1";
return $count;
}

}

//Return count value to be displayed on page
$num = get_count($PHP_SELF);
echo "$num";
?>                    

Comments or questions?
PX is running PHP 5.2.17
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.