limit page request with php
I have this code that prevents users from making multiple reques开发者_Go百科ts on a page and it records ip address in a text file along with a string of numbers i assume its a time stamp but im not sure looks like this 173.1.1.100 - Requested on: 1303521541
how do i change that to readable time with a date?
$ipLog='log.txt'; //Logfile
$timeout='1'; //Wait Time
$goHere=$headers; //Page To Access
$register_globals = (bool) ini_get('register_gobals');
if ($register_globals) $vis_ip = getenv(REMOTE_ADDR);
else $vis_ip = $_SERVER['REMOTE_ADDR'];
function recordData($vis_ip,$ipLog,$goHere)
{
$log=fopen("$ipLog", "a+");
fputs ($log,$vis_ip." - Requested on: ".time()."\n");
fclose($log);
//
//
//
exit(0);
}
function checkLog($vis_ip,$ipLog,$timeout)
{
global $valid; $ip=$vis_ip;
$data=file("$ipLog"); $now=time();
foreach ($data as $record)
{
$subdata=explode(" - Requested on: ",$record);
if ($now < ($subdata[1]+600*$timeout) && $ip == $subdata[0])
{
$valid=0; echo "$timeout min wait.";
break;
}
}
}
checkLog($vis_ip,$ipLog,$timeout);
if ($valid!="0") recordData($vis_ip,$ipLog,$goHere);
When file has been saved, save a date
fputs ($log,$vis_ip." - Requested on: ".date("Y-M-d H:m:s")."\n");
when its extracted , revert to timestamp and keep the logic
$subdata=explode(" - Requested on: ",$record);
$subdata[1]= strtotime($subdata[1]); //convert date to timestamp
精彩评论