我收到一个我无法重现的错误。
以下代码是防止攻击的模块的一部分。这个特定的片段正在跟踪我得到的特定机器人用户代理的点击次数。
经过多年无故障使用后,我突然收到错误:
遇到非格式良好的数值;
这发生在以下行:
$seconds = time() - $time;
$ time的值为2016-10-02 19:33:42
函数safefilename()返回:
Mozilla的5-0兼容-spbot-5-0-3-HTTP-OpenLinkProfiler-ORG-机器人
正在写入和读取的文件的名称是:
bot_2016-10-02--19-33-42_Mozilla-5-0兼容-spbot-5-0-3-HTTP-Open_104.131.179.5.log
方法
下面的代码定位机器人并写入基于用户代理和文件创建时间的文件名。每次使用该用户代理时,它都会添加一个" X"到文件,所以我可以跟踪代理访问了多少次。如果机器人瞄准我超过一定次数,我会阻止它。
下面的代码在测试和生产中产生了期望的结果 - 当然除非抛出此错误。提到的文件有6个字节写入,所以它已被成功读取和写入5次。
在06:37:04记录了php错误,我的服务器日志文件显示了这些命中:
104.131.63.140 - - [10/Dec/2016:06:36:59 -0800] "GET /robots.txt HTTP/1.1" 301 257 "-" "Mozilla/5.0 (compatible; spbot/5.0.3; +http://OpenLinkProfiler.org/bot )"
104.131.63.140 - - [10/Dec/2016:06:36:59 -0800] "GET /robots.txt HTTP/1.1" 200 1460 "-" "Mozilla/5.0 (compatible; spbot/5.0.3; +http://OpenLinkProfiler.org/bot )"
104.131.63.140 - - [10/Dec/2016:06:37:04 -0800] "GET / HTTP/1.1" 403 937 "-" "Mozilla/5.0 (compatible; spbot/5.0.3; +http://OpenLinkProfiler.org/bot )"
104.131.63.140 - - [10/Dec/2016:06:37:05 -0800] "GET / HTTP/1.1" 301 247 "-" "Mozilla/5.0 (compatible; spbot/5.0.3; +http://OpenLinkProfiler.org/bot )"
PHP代码 我已经提取了以下代码,可以单独运行以进行测试。
// this is my site address
define("STATIC_SITE_ROOT", "http://static");
$agent = "Mozilla/5.0 (compatible; spbot/5.0.3; +http://OpenLinkProfiler.org/bot )";
$ip = '127.0.0.1';
$t = new test();
$t->testAgent($agent, $ip);
class test {
public $agent;
public $ip;
public $maxbadpages = 100;
function testAgent($agent, $ip){
$this->agent = $agent;
$this->ip = $ip;
if (strlen($badbot = $this->badbot($this->agent)) > 0){
$new = FALSE;
$path = $_SERVER['DOCUMENT_ROOT'] . "/logs";
// $filename = "bot-" . time() . "-" . safefilename(substr($this->agent, 0, 50));
$safefilename = safefilename(substr($this->agent, 0, 50));
$filename = "bot_" . date("Y-m-d--H-i-s") . "_" . $safefilename . "_" . $this->ip . ".log";
$filter = $safefilename;
$afiles = getDirArray($path, $filter);
if (count($afiles) > 0){
// bot file already exists
$filename = $afiles[0];
} else {
// add time to filename if crating new file
$new = TRUE;
}
$fullfilename = "$path/$filename";
// log a counter (# bytes in file)
file_put_contents($fullfilename, "X", FILE_APPEND);
// number of hits == size of file
$size = filesize($fullfilename);
// count hits to determine if block via htaccess
// if > # entries in log from a useragent, ban it
if ($size > $this->maxbadpages){
$this->blockagent($this->agent, $this->ip, "> $this->maxbadpages hits");
} elseif (! $new) {
// test for hits per second
$blockagent = FALSE;
$parts = explode("_", $filename);
// 2nd part is the time
// $time = strtotime($parts[1]);
$parts2 = explode("--", $parts[1]);
$time = $parts2[0] . " " . str_replace("-",":",$parts2[1]);
// seconds is time elapsed
$seconds = time() - $time;
// check for various scenarios
if ($size > $seconds * 2){
// more than average of 2 hits per second for any period
$blockagent = TRUE;
$reason = "$size (hits) > $seconds (seconds) * 2";
}
if ($seconds >= 10 && $size > $seconds * 1){
// more than 1 hit per second over 10 seconds
$blockagent = TRUE;
$reason = "$seconds (seconds) >= 10 && $size (hits) > $seconds (seconds) * 1";
}
if ($blockagent){
$this->blockagent($this->agent, $this->ip, $reason);
}
}
$this->blockAccess("bad bot: ". $badbot);
}
}
function blockAgent($message){
die("Block Agent: " . $message);
}
function blockAccess($message){
die("Block Access: " . $message);
}
function badbot($agent) {
if (stripos($agent, "bot") !==FALSE){
return "match 'bot' in agent: ($agent)";
} elseif (stripos($agent, "spider") !==FALSE){
return "match 'spider' in agent: ($agent)";
} elseif (stripos($agent, "crawl") !==FALSE){
return "match 'crawl' in agent: ($agent)";
}
$badbots = array(
"007AC9",
"2Bone",
"404 Checker",
"There are many more bad bots contained in this array...");
foreach ($badbots as $bot) {
//If the spider text is found in the current user agent, then return true
if (stripos($agent, $bot) !== false){
return "$bot ($agent)";
return "match: $bot in agent: ($agent)";
}
}
//If it gets this far then no bot was found!
return "";
}
}
function safefilename($string){
// convert entities e.g. Á => Á
$string = htmlentities($string, ENT_QUOTES, 'UTF-8');
// replace the entities with letter equivalents
$string = preg_replace('~&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', $string);
// return entities which did not have letter equivalents back to entities
$string = html_entity_decode($string, ENT_QUOTES, 'UTF-8');
// replace non valid chars with dash and multiple dashes with only one
$string = preg_replace(array('~[^0-9a-z]~i', '~[ -]+~'), '-', $string);
return trim($string, ' -');
}
function getDirArray($path = "./", $filter = ".*", $exclude = '', $sorted = true, $optfilter2 = '') {
// for server directories, can't use the static url
$path = str_replace(STATIC_SITE_ROOT, $_SERVER['DOCUMENT_ROOT'], $path);
if (file_exists($path) == false) {
if (mkdir($path, 0777, true) == false) {
die($path);
exit;
}
}
$handle = opendir($path);
$dir = array();
while ($file = readdir($handle)) {
if (is_file("$path/$file") && preg_match("/$filter/", $file) && (strlen($exclude) == 0 ? TRUE : !preg_match("/$exclude/", $file))) {
if ($optfilter2 == '') {
// No 2n filter
$dir[] = $file;
} else {
$pos = strpos($file, $optfilter2);
if ($pos === false) {
// Not found
} else {
$dir[] = $file;
}
}
}
}
closedir($handle);
if ($sorted == true) {
sort($dir);
}
return $dir;
}
答案 0 :(得分:3)
问题是您使用的是日期时间字符串而不是unix时间戳。根据我的评论中的建议,您需要使用strtotime($time)
来解决此问题,但您似乎并不理解原因。
来自time
的文档:
返回自Unix Epoch(1970年1月1日00:00:00 GMT)以来秒数测量的当前时间。
这意味着当你执行time()
时,它会返回秒数 - 一个整数 - 自GMT时区的1970年新年以来。
另一方面,你有$time
,这是一个字符串。这个字符串是一个更加用户友好的字符串,可以读取而不是一个表示秒数的整数。在某些情况下,您需要此字符串而不是unix时间戳,尽管这次不是这种情况。
您试图从$time
(整数)中减去time()
(字符串)。这显然不起作用,因为你不能从一个数字中减去一个字母,这就是你得到那个错误的原因。 strtotime
是一个能够将日期解析为字符串的函数,例如您提供的字符串,并将其转换为自1970年新年以来的秒数整数。
在您的评论中,您在$time
中加入strtotime()
之后说,您现在获得5937340
作为结果。这是当前时间与$time
之间的秒数差异。希望这是你想要的。相当于大约68.7天。如果这不是您预期的结果,那么我可以尝试进一步帮助您。
也可以使用DateTime
类相互减去两个日期字符串,但在我看来,这种情况更复杂,更不必要。但是,您不能从字符串日期中减去整数日期。它们必须转换为相同类型。希望我能帮你解决这个问题。