# This is the directory and file in which the script will keep its data files. # he directory must be chmod 777, or what is needed to ensure the server can write # to it. You must create the directory (but not the file) before running this script. $file = "/tmp/counter.txt"; # This is the directory and file in which the script will keep its history files. # The directory must be chmod 777, or what is needed to ensure the server can write # to it. You must create the directory (but not the file) before running this script. $log = "/tmp/log.txt"; # If your system can use "flock" to lock data files, set this to 1. # Otherwise, set it to zero. If you are unsure, try 1 first, and if # the script is failing try setting it to 0. $locking = 1; # If this is set to 1, the last 5 IP addresses to access the counter will be # cached to prevent one person from repeatedly updating the counter. If # it is set to 0, one person can increment the counter multiple times in # succession. $ip_caching = 0; # If you do not want to count certain visitors, such as yourself, # simply add the IP addresses to be blocked. $no_count = array("168.45.58.39", "67.80.70.211"); # This is the format in which the output will be result. # If you want the total hits your page has had to be displayed, set $show_overall = 1. # If you want the total year's worth of hits for your page to be displayed, set $show_yearly = 1. # If you want the total month's worth of hits for your page to be displayed, set $show_monthly = 1. # If you want the total week's worth of hits for your page to be displayed, set $show_weekly = 1. # If you want the total day's worth of hits for your page to be displayed, set $show_daily = 1. # If you want a hidden counter, set all values to "0". $show_overall = 0; $show_yearly = 0; $show_monthly = 0; $show_weekly = 0; $show_daily = 0; # ##### YOU DO NOT HAVE TO EDIT BELOW THIS LINE (BUT YOU CAN IF YOU WANT TO)###### init(); function init(){ if (validate()){ global $file; if (!file_exists($file)){ global $locking; $fp = fopen($file, "w") or error("creating file"); if ($locking){ flock($fp, 2) or error("locking file"); } $dstamp = dstamp(); $wstamp = wstamp(); $ip = getenv('REMOTE_ADDR'); fwrite($fp, "1:1:1:1:1:$dstamp:$wstamp:$ip:$ip:$ip:$ip:$ip"); if ($locking == 1){ flock($fp, 3); } fclose($fp); chmod($file, 0777); return init_report(); } else{ return update(); } } } function validate(){ global $no_count; $ip = getenv('REMOTE_ADDR'); $size = count($no_count); for ($i = 0; $i < $size; $i++){ if ($ip == $no_count[$i]){ return 0; } return 1; } } function update(){ global $file; global $locking; global $ip_caching; @ $fp = fopen($file, "r") or error("reading file"); if ($locking){ flock($fp, 2) or error("locking file"); } $contents = fgetcsv($fp, 150, ":"); fclose($fp); $overall = $contents[0]; $daily = $contents[1]; $weekly = $contents[2]; $monthly = $contents[3]; $yearly = $contents[4]; $fdstamp = $contents[5]; $fwstamp = $contents[6]; $ip1 = $contents[7]; $ip2 = $contents[8]; $ip3 = $contents[9]; $ip4 = $contents[10]; $ip5 = $contents[11]; $dstamp = dstamp(); $wstamp = wstamp(); $y = substr($dstamp, 0, 4); $m = substr($dstamp, 4, 2); $fy = substr($fdstamp, 0, 4); $fm = substr($fdstamp, 4, 2); if ($fy < $y){ $yearly = 0; } if ($fm < $m){ $monthly = 0; } if ($fdstamp < $dstamp){ logit($daily); $daily = 0; $fdstamp = $dstamp; } if ($fwstamp < $wstamp){ $weekly = 0; $fwstamp = $wstamp; } $ip_visitor = getenv("REMOTE_ADDR"); if ((!$ip_caching) || ($ip_caching && ($ip_visitor != $ip1) && ($ip_visitor != $ip2) && ($ip_visitor != $ip3) && ($ip_visitor != $ip4) && ($ip_visitor != $ip5))) { $overall++; $daily++; $weekly++; $monthly++; $yearly++; $ip1 = $ip2; $ip2 = $ip3; $ip3 = $ip4; $ip4 = $ip5; $ip5 = getenv('REMOTE_ADDR'); } @ $fp = fopen($file, "w") or error("creating file"); if ($locking){ flock($fp, 2) or error("locking file"); } fwrite($fp, "$overall:$daily:$weekly:$monthly:$yearly:$fdstamp:$fwstamp:$ip1:$ip2:$ip3:$ip4:$ip5"); if ($locking == 1){ flock($fp, 3); } fclose($fp); report($overall,$daily,$weekly,$monthly,$yearly); } function dstamp(){ $day = date("d"); $mon = date("m"); $year = date("Y"); return "$year$mon$day"; } function wstamp(){ $check = date("w"); $check = 7 - $check; $jump = $check * 86400; $timestamp = date("U"); $timestamp += $jump; $day = date("d", $timestamp); $mon = date("m", $timestamp); $year = date("Y", $timestamp); return "$year$mon$day"; } function report($overall,$daily,$weekly,$monthly,$yearly){ global $show_overall; global $show_yearly; global $show_monthly; global $show_weekly; global $show_daily; if ($show_overall == 1 || $show_yearly == 1 || $show_monthly == 1 || $show_weekly == 1 || $show_daily == 1) echo "Hits "; if ($show_yearly) echo "this YEAR: $yearly, "; if ($show_monthly) echo "this MONTH: $monthly, "; if ($show_weekly) echo "this WEEK: $weekly, "; if ($show_daily) echo "this DAY: $daily, "; if ($show_overall) echo "OVERALL: $overall."; } function init_report() { echo "Hits this YEAR: 1, MONTH: 1, WEEK: 1, TODAY: 1, OVERALL: 1."; } function logit($daily){ global $log; if (!file_exists($log)){ global $locking; $fp = fopen($log, "w") or error("creating file"); fclose($fp); chmod($log, 0777); } $date = date("U"); $date = $date - 86400; $date = date("l, F d, Y", $date); @ $fpl = fopen($log, "a") or error("opening log"); flock($fpl, 2) or error("locking file"); fwrite($fpl, "$date: $daily\n"); flock($fpl, 3); fclose($fpl); } ?>