PHP script that counts visits on webpages and displays statistics as HTML/CSS
if a logfile analyzer solution is not suiteful. All data is stored in textfiles so no
MySQL database is needed. IP adresses of visitors are stored a specific time to
prevent multiple counting.
back to top
Include the PHP Code into a PHP file or insert it in any HTML code and rename
the file from <name>.html to <name>.php
Edit the 'preferences' section at the top of the file:
The path's of the two data files must be writable; the files will be generated
automatically. Set 'start_time' to the date that should be displayed in the output.
Set 'startnum' to an initial value if the counter should not start with zero. Now
you can use both the variables $y (the counter) and $start_time at any point in the file.
Simply write into the HTML code: <?php print $y; ?> and <?php print $start_time; ?>.
back to top
Every time the file with the script is viewed, the counter checks if the IP adress
of the visitor is stored. If yes, it decides upon the setting 'maxip_time' if the
counter gets incremented or not. IP adresses older than 'maxip_time' are deleted
regularly. With the setting 'max_ip' the maximum number of stored IP adresses is
specified. Both the settings can be altered arbitrarily after the first run.
back to top
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
back to top
<?php
// Visit Counter - PHP based visit counter for websites
// last changed: July 17. 2003
// Copyright (C) 2004 Soenke Gluch
// http://www.mellowmood.no-ip.com
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//preferences
$countfile = "/data/counter_count.txt"; //file with counter
$ipfile = "/data/counter_ip.txt"; //file with iplist
$maxip = "100"; //maximum number of saved ips
$maxip_time = "1440"; //duration of ip-blocking (in minutes)
$startnum = "441"; //initial count
$start_time = "12.09.04"; //can be used for output
function zaehlen($countfile) { //counter inkrementieren
global $nummer;
$oeffnen = fopen($countfile, "r-");
$nummer = fread($oeffnen, filesize($countfile));
fclose($oeffnen);
$nummer++;
$oeffnen = fopen($countfile, "w-");
fwrite($oeffnen, $nummer);
fclose($oeffnen);
}
$maxip_time = $maxip_time * 60;
$userip = $_SERVER['REMOTE_ADDR'];
$current_time = substr(strrchr(microtime(), " "), 1);
if(!file_exists($countfile)){ //neue datei mit initialisiertem z�hler anlegen falls nit da
$fo = fopen($countfile,"w-");
fputs($fo, $startnum);
fclose($fo);
}
if(file_exists($ipfile)){ //ipfile existiert
$ipcont = fopen($ipfile,"r-"); //ipfile in array packen
$ipfile1 = fgets($ipcont,500);
fclose($ipcont);
$ipdata = explode("|",$ipfile1);
if(in_array($userip,$ipdata)){ //ip ist im ipfile
$new_ipdata = array();
for ($i=0;$i<sizeof($ipdata)/2;$i++) { //array neu aufbauen
if(current($ipdata) == $userip) { //datensatz enthaelt ip
array_unshift($new_ipdata, $userip, $current_time); //ip mit aktueller zeit an den an
fang packen
next($ipdata);
$old_time = $ipdata[key($ipdata)];
if($current_time-$old_time > $maxip_time){ //falls timestamp zu alt, zaehlen und arra
y abschliessen
zaehlen($countfile);
break;
}
else { //timestamp aktuell
$anzahl = fopen($countfile,"r-"); //ausgabestring gleich bisherige hits setzen
$nummer = fgets($anzahl,9);
fclose($anzahl);
}
next($ipdata);
}
else { //datensatz enthaelt ip nicht
$ip = current($ipdata);
if($current_time - next($ipdata) < $maxip_time){ //falls timastamp aktuell, ins neue
array uebernehmen
array_push($new_ipdata, $ip, current($ipdata));
}
next($ipdata);
}
}
$ipdata = implode("|",$new_ipdata); //ipdata wieder in datei schreiben
$ipcont = fopen($ipfile,"w-");
fputs($ipcont,$ipdata);
fclose($ipcont);
}
else { //ip ist nicht im ipfile
while (count($ipdata) >= ($maxip*2)) { //falls ipfile voll �lteste ips rauswerfen
array_pop($ipdata);
array_pop($ipdata);
}
array_unshift($ipdata,$userip,$current_time); //timestamp anf�gen
$userip2 = implode("|",$ipdata); //ipdata wieder in datei schreiben
$ipcont = fopen($ipfile,"w-");
fputs($ipcont,$userip2);
fclose($ipcont);
zaehlen($countfile);
}
}
else { //ipfile existiert nicht
$foip = fopen($ipfile, "w-");
fputs($foip, $userip."|".$current_time); //ip mit zeitstempel in datei speichern
fclose($foip);
zaehlen($countfile);
}
?>