Php 遇到格式不正确的数值-无法复制
我遇到了一个无法重现的错误 以下代码是防止攻击的模块的一部分。这个特定的代码片段跟踪我从特定的bot用户代理获得的点击量 经过多年的无故障使用,我突然发现了一个错误: 遇到格式不正确的数值 这发生在以下位置:Php 遇到格式不正确的数值-无法复制,php,php-7.1,Php,Php 7.1,我遇到了一个无法重现的错误 以下代码是防止攻击的模块的一部分。这个特定的代码片段跟踪我从特定的bot用户代理获得的点击量 经过多年的无故障使用,我突然发现了一个错误: 遇到格式不正确的数值 这发生在以下位置: $seconds = time() - $time; $time的值是2016-10-0219:33:42 函数safefilename()返回: Mozilla-5-0-compatible-spbot-5-0-3-http-OpenLinkProfiler-org-bot 正
$seconds = time() - $time;
$time的值是2016-10-0219:33:42
函数safefilename()返回:
Mozilla-5-0-compatible-spbot-5-0-3-http-OpenLinkProfiler-org-bot
正在写入和读取的文件的名称为:
bot_2016-10-02-19-33-42_Mozilla-5-0-compatible-spbot-5-0-3-http-Open_104.131.179.5.log
方法学
下面的代码以机器人程序为目标,并写入基于用户代理和文件创建时间的文件名。每次使用该用户代理时,它都会在文件中添加一个“X”,这样我就可以跟踪该代理访问了多少次。如果一个机器人瞄准我超过一定次数,我会阻止它
下面的代码在测试和生产中产生了期望的结果——当然,抛出此错误时除外。上面提到的文件有6个字节写入,所以之前已经成功读写了5次
php错误记录在06:37:04,我的服务器日志文件显示了以下点击:
104.131.63.140---[10/Dec/2016:06:36:59-0800]“GET/robots.txt HTTP/1.1”301 257“-”Mozilla/5.0(兼容;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(兼容;spbot/5.0.3+http://OpenLinkProfiler.org/bot )“
104.131.63.140---[10/Dec/2016:06:37:04-0800]“GET/HTTP/1.1”403937“-”Mozilla/5.0(兼容;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(兼容;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;
}
问题是您使用的是日期时间字符串,而不是unix时间戳。正如我在评论中所建议的,您需要使用
strotime($time)
来解决这个问题,但您似乎不明白为什么
从时间的文档中
:
返回自Unix纪元(1970年1月1日00:00:00 GMT)以来以秒为单位测量的当前时间
这意味着当您执行time()
时,它将返回自1970年新年以来在GMT时区内的秒数(一个整数)
另一方面,您有一个$time
,它是一个字符串。这个字符串是一个更易于用户阅读的字符串,而不是一个表示秒数的整数。在某些情况下,您需要这个字符串而不是unix时间戳,尽管这次不是这样
您试图从time()
(整数)中减去$time
(字符串)。这显然是行不通的,因为你不能从一个数字中减去一个字母,这就是为什么你会出现这个错误strotime
是一个函数,它能够将日期解析为字符串(如您提供的字符串),并将其转换为自1970年新年以来秒数的整数
您在评论中说,在将$time
封装在strotime()
中之后,您现在得到的结果是5937340
。这是当前时间与$time
之间的秒差。希望这就是你想要的。这相当于大约68.7天。如果这不是你期望的结果,那么我可以尝试进一步帮助你
也可以使用
DateTime
类从彼此之间减去两个日期字符串,但在我看来,在您的情况下,这更复杂,也没有必要。但是,不能从字符串日期中减去整数日期。它们必须转换为同一类型。希望我能帮你解决这个问题 问题是您使用的是日期时间字符串,而不是unix时间戳。正如我在评论中所建议的,您需要使用strotime($time)
来解决这个问题,但您似乎不明白为什么
从时间的文档中
:
返回自Unix纪元(1970年1月1日00:00:00 GMT)以来以秒为单位测量的当前时间
这意味着当您执行time()
时,它将返回自1970年新年以来在GMT时区内的秒数(一个整数)
另一方面,您有一个$time
,它是一个字符串。这个字符串是一个更易于用户阅读的字符串,而不是一个表示秒数的整数。在某些情况下,您需要这个字符串而不是unix时间戳,尽管这次不是这样
您试图从time()
(整数)中减去$time
(字符串)。这显然是行不通的,因为你不能从一个数字中减去一个字母,这就是为什么你会出现这个错误strotime
是一个函数,它能够将日期解析为字符串(如您提供的字符串),并将其转换为自1970年新年以来秒数的整数
您在评论中说,在将$time
封装在strotime()
中之后,您现在得到的结果是5937340
。这是当前时间与$time
之间的秒差。希望这就是你想要的。这相当于大约68.7天。如果这不是你期望的结果,那么我