用php限制页面请求

时间:2011-04-23 02:56:05

标签: php

我有这个代码阻止用户在页面上发出多个请求,它会在文本文件中记录IP地址以及一串数字我假设它是一个时间戳但我不确定看起来像这样173.1.1.100 - Requested on: 1303521541如何将其更改为具有日期的可读时间?

$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); 

1 个答案:

答案 0 :(得分:3)

保存文件后,保存日期

 fputs ($log,$vis_ip." - Requested on: ".date("Y-M-d H:m:s")."\n"); 

提取时,恢复时间戳并保持逻辑

$subdata=explode(" - Requested on: ",$record);
$subdata[1]= strtotime($subdata[1]); //convert date to timestamp