PHP警告:preg_match():找不到结束分隔符'^'

时间:2017-12-13 04:46:55

标签: php preg-match

这是我的完整BlockBot.php

<?
$bannedIP = array("^50.116.25.17", "^124.178.234.95", "^174.122.201.154", "^66.102.*.*", "^66.249.*.*", "^72.14.192.*", "^74.125.*.*", "^209.85.128.*", "^216.239.32.*", "^74.125.*.*", "^207.126.144.*", "^173.194.*.*", "^64.233.160.*", "^72.14.192.*", "^66.102.*.*", "^64.18.*.*", "^194.52.68.*", "^194.72.238.*", "^62.116.207.*", "^212.50.193.*", "^69.65.*.*", "^50.7.*.*", "^131.212.*.*", "^46.116.*.* ", "^62.90.*.*", "^89.138.*.*", "^82.166.*.*", "^85.64.*.*", "^85.250.*.*", "^89.138.*.*", "^93.172.*.*", "^109.186.*.*", "^194.90.*.*", "^212.29.192.*", "^212.29.224.*", "^212.143.*.*", "^212.150.*.*", "^212.235.*.*", "^217.132.*.*", "^50.97.*.*", "^217.132.*.*", "^209.85.*.*", "^66.205.64.*", "^204.14.48.*", "^64.27.2.*", "^67.15.*.*", "^202.108.252.*", "^193.47.80.*", "^64.62.136.*", "^66.221.*.*", "^64.62.175.*", "^198.54.*.*", "^192.115.134.*", "^216.252.167.*", "^193.253.199.*", "^69.61.12.*", "^64.37.103.*", "^38.144.36.*", "^64.124.14.*", "^206.28.72.*", "^209.73.228.*", "^158.108.*.*", "^168.188.*.*", "^66.207.120.*", "^167.24.*.*", "^192.118.48.*", "^67.209.128.*", "^12.148.209.*", "^12.148.196.*", "^193.220.178.*", "68.65.53.71", "^198.25.*.*", "^64.106.213.*", "^91.103.66.*", "^208.91.115.*");
$blocked_words = array("above","google","softlayer","amazonaws","cyveillance","phishtank","dreamhost","netpilot","calyxinstitute","tor-exit", "msnbot","p3pwgdsn","netcraft","trendmicro","phishlabs");
//----------------------++
$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
foreach($blocked_words as $word) {
    if (substr_count($hostname, $word) > 0) {
        header("HTTP/1.0 404 Not Found");
        die("<h1>404 Not Found</h1>The page that you have requested could not be found.");
        exit();
    }  
}
//----------------------++
if(in_array($_SERVER['REMOTE_ADDR'],$bannedIP)) {
     header('HTTP/1.0 404 Not Found');
     exit();
} else {
     foreach($bannedIP as $ip) {
          if(preg_match($ip,$_SERVER['REMOTE_ADDR'])) {
               header('HTTP/1.0 404 Not Found');
               die("<h1>404 Not Found</h1>The page that you have requested could not be found.");
               exit();
          }
     }
}
?>

ERRORLOG.TXT

  

PHP警告:preg_match():找不到结尾分隔符'^'   第33行/home/SITES/SITES/BlockBot.php

ERRORLOG.TXT

  

PHP警告:preg_match():分隔符不能是字母数字或   第33行/home/SITES/SITES/BlockBot.php中的反斜杠

有人请帮我解决这个问题,或者告诉我如何解决这个问题。

之前很多。

2 个答案:

答案 0 :(得分:0)

您必须从IP列表中转义特殊字符。其他正则表达式由于特殊字符而失败(点是特殊字符)。此外,你不能把*放在ip列表中,这将匹配&#34; *&#34; 示例:

$bannedIP = array("/50\.116\.25\.17/", "/124\.178\.234\.95/", "/174\.122\.201\.154/","/212\.143\.*\.*/");

OR

$bannedIP = array("/50\.116\.25\.17/", "/124\.178\.234\.95/", "/174\.122\.201\.154/","/212\.143\.\d{1,3}\.\d{1,3}/");

答案 1 :(得分:0)

用作正则表达式的字符串的第一个字符被视为分隔符,PHP将查找其匹配项以了解正则表达式何时停止并且标志开始。

按照惯例,这个分隔符通常是/,但它不一定是;你可以从"any non-alphanumeric, non-backslash, non-whitespace character"开始。如果您使用字符作为具有自然对的分隔符(例如,[),PHP将查找该对的结束字符(例如,])。否则,它会查找相同的字符。

在您的情况下,您使用以^开头的字符串作为正则表达式(在preg_match()调用中),但您没有提供相应的结束分隔符(^)在字符串的末尾,这是PHP错误所抱怨的。

您的数组(查看前两个元素)应该是:

$bannedIP = array("^50.116.25.17^", "^124.178.234.95^");

以便它包含匹配的分隔符。但是,我认为你可能试图将匹配锚定在字符串的开头,在这种情况下你需要提供不同的分隔符(在我的例子中我使用的是/) 。如果您在开头指定一个锚点,我假设您还想在末尾指定一个锚点($字符,当用作正则表达式的最后一个字符时):

$bannedIP = array("/^50.116.25.17$/", "/^124.178.234.95$/");

您需要混合来自BRjava的建议:转义.个字符以使它们与文字点匹配,并且还匹配1-3个数字的序列,而不是{{ 1}}字符(表示“尽可能多次重复最后一个字符”):

*

最后,您在两个不兼容的上下文中使用了$bannedIP = array("/^50\.116\.25\.17$/", "/^124\.178\.234\.95$/", "/^212\.143\.\d{1,3}\.\d{1,3}$/"); 数组;搜索与$bannedIP匹配的值,它将进行字符串比较,并与in_array()匹配,这将进行正则表达式匹配。您将希望坚持使用一种格式或另一种格式,因为如果您的数组具有不打算用作正则表达式的字符串,则preg_match()调用将失败。