#!/usr/bin/php #This won't work if your server doesn't recognize .php as executable. #Or, you can call it from within a PHP document, using the PHP command #include('/path/to/count.php') # You'll need to modify a couple of the variables below: #$datadir is the directory where the counter log files will be kept. #You should probably make this a dedicated directory, because a log #file will be created for each page on which you're using this script. #This needs to be the full system path to your data directory, including #the trailing slash. This directory MUST EXIST with 755 permissions in #order for the script to function. $datadir = "/home/bv125843/public_html/shaun/00/counters/"; #If $snoop is set to 1, the counter will also keep a log of each visitor's #IP address, browser type/version, and referring URL if available. #The snoop logs are kept in the same directory as the counter logs. #Watch out, if you have a high-traffic site, these logs grow fast. $snoop = 1; ################################################################### # DO NOT EDIT BELOW THIS LINE, unless you know what you're doing! # ################################################################### #Until I rewrite my scripts, this will suffice for bg-compatibility if(phpversion() >= "4.2.0"){ extract($_POST); extract($_SERVER); extract($_ENV); } # #do_snoop() records the IP address, browser type, and referring #URL of the visitor, if $snoop is set to 1. # function do_snoop($datadir,$REMOTE_ADDR,$HTTP_USER_AGENT,$HTTP_REFERER,$REQUEST_URI) { $filename = str_replace("/","_",$REQUEST_URI); if ( (substr($filename, -1)) == "_" ) $filename .= "index"; $date = date("m/d/Y h:i:s A"); $logfile = fopen("$datadir" . "$filename" . "-info.txt", "a+"); fputs("$logfile", $date . "\t" . $REMOTE_ADDR . "\t" . $HTTP_USER_AGENT . "\t " . $HTTP_REFERER . "\n"); fclose($logfile); } #MAIN PROGRAM CODE BEGINS HERE #Get the name of the page being viewed, apache should send #this through REQUEST_URI. The file name is then unslashed #(the clean version is used in the name of the log file). $filename = str_replace("/","_",$REQUEST_URI); if ( (substr($filename, -1)) == "_" ) $filename .= "index"; #Check to see if a counter log already exists for this page $filetest = file_exists( "$datadir" . "$filename"); if (!$filetest) { # If there's not a log file for this page, try to create one $logfile = fopen( "$datadir" . "$filename", "w"); if (!$logfile) exit; else { $count += 1; # increment count from 0 to 1 fputs("$logfile", $count); # write count to log file fclose("$logfile"); exit; } } else #If a log file is already present, read the current count $logfile = fopen("$datadir" . "$filename", "r+"); if (!$logfile) exit; else { $count = fgets("$logfile", 8); if (!$count) exit; else { $count += 1; #increment the count by 1 #This is where the script will send the count to the calling web page, #if it's working properly. If you want to give the counter a consistent #look across all your pages, you can edit the following echo statement, #and add HTML tags before and after the variable name. For example: #echo " $count "; echo "$count"; fseek("$logfile", 0); #go to beginning of log fputs("$logfile", $count); #replace old count with new one fclose($logfile); #close log file } } #Snoop the visitor, if we want to if ($snoop==1) do_snoop($datadir,$REMOTE_ADDR,$HTTP_USER_AGENT,$HTTP_REFERER,$REQUEST_URI); #And we're done! exit; ?>