<?php
define("COUNTERDIR", "data/");
define("DEBUG", "off");
define("TIMEOUT", 1800);
class counter
{
function counter()
{
if(DEBUG == "on")
echo "counter->counter()<br>\n";
//read IP Table
$ipTable = join("|", file(COUNTERDIR . "ipTable.txt"));
//check if IP ist allready in IP table
if(!stristr($ipTable, getenv("REMOTE_ADDR")))
{
$IPin = "false";
$counterValue = $this->getCounterValue();
$counterValue = $counterValue + 1;
$this->writeCounterFile($counterValue);
$this->insertIntoIPTable(getenv("REMOTE_ADDR"));
$this->rebuildIPTable();
}
else
{
$IPin = "true";
$this->rebuildIPTable();
}
if(DEBUG == "on")
echo "counter->counter($IPin)->DONE<br>\n";
}
function getCounterValue()
{
if(DEBUG == "on")
echo "counter->getCounterValue()<br>\n";
$file = openFile(COUNTERDIR . "counter.txt", "r+");
$counterValue = fgets($file, 10);
closeFile($file);
if(DEBUG == "on")
echo "counter->getCounterValue()->DONE<br>\n";
return $counterValue;
}
function writeCounterFile($value)
{
if(DEBUG == "on")
echo "counter->writeCounterFile()<br>\n";
$file = openFile(COUNTERDIR . "counter.txt", "w");
fputs($file, $value);
closeFile($file);
if(DEBUG == "on")
echo "counter->writeCounterFile()->DONE<br>\n";
}
function insertIntoIPTable($ip)
{
if(DEBUG == "on")
echo "counter->insertIntoIPTable()<br>\n";
$file = openFile(COUNTERDIR . "ipTable.txt", "a+");
fputs($file, $ip . "|" . time() . "|\n");
closeFile($file);
if(DEBUG == "on")
echo "counter->insertIntoIPTable()->DONE<br>\n";
}
function rebuildIPTable()
{
if(DEBUG == "on")
echo "counter->rebuildIPTable()<br>\n";
$ipTable = file(COUNTERDIR . "ipTable.txt");
$writeToFile = "";
for($i = 0; $i < count($ipTable); $i++)
{
$data = explode("|", "$ipTable[$i]");
if(($data[1] >= (time() - TIMEOUT)) && ($data[0] != ""));
{
$writeToFile .= $data[0] . "|" . $data[1] . "|\n";
}
}
$file = openFile(COUNTERDIR . "ipTable.txt", "w");
fputs($file, $writeToFile);
closeFile($file);
if(DEBUG == "on")
echo "counter->rebuildIPTable()->DONE<br>\n";
}
}
?>