在txt内容php中查找ip

在txt内容php中查找ip,php,ip,Php,Ip,我有一个文本文件:ban.txt有内容吗 a:5:{i:14528;s:15:" 118.71.102.176";i:6048;s:15:" 113.22.109.137";i:16731;s:3:" 118.71.102.76";i:2269;s:12:" 1.52.251.63";i:9050;s:14:"123.21.100.174";} 我写了一个脚本来查找和禁止这个txt中的IP <?php $banlist = file("ban.txt"); foreach($banl

我有一个文本文件:ban.txt有内容吗

a:5:{i:14528;s:15:" 118.71.102.176";i:6048;s:15:" 113.22.109.137";i:16731;s:3:"  118.71.102.76";i:2269;s:12:" 1.52.251.63";i:9050;s:14:"123.21.100.174";}
我写了一个脚本来查找和禁止这个txt中的IP

<?php
$banlist = file("ban.txt");

foreach($banlist as $ips ) {

    if($_SERVER["REMOTE_ADDR"] == $ips) {
        die("Your IP is banned!");
    }
}
?>

可以帮我列出这个内容的IP,我是一个php新手。非常感谢

看,这是一个公认的垃圾解决方案,基于一个不清楚的问题

Regex似乎从来都不是一个很好的解决方案,但我没有太多关于文件一致性的细节

一,。隔离ban.txt中的“s”段

因此,我的正则表达式并不美妙,但这个正则表达式应该与“s”段相匹配,而“s”段似乎是针对IP禁令的(尽管您的评论说“IP总是在“IP”中”有点混淆)

二,。隔离每个“s”段内的IP

一旦我们有了这些段,我们就可以将起始位剥离到实际IP(即将
s:123:“192.168.0.0”
转换为
192.168.0.0”
),然后修剪结束引号和分号(即
192.168.0.0”
192.168.0.0
):

三,。示例代码

这将为我们提供以下PHP代码:

$banText = file_get_contents("ban.txt"); 

/* Evil, evil regexes */
$sSegmentsRegex = '/s:[0-9]+:"[ ]*[0-9]+.[0-9]+.[0-9]+.[0-9]+"/';
$removeStartJunkRegex = '/s:[0-9]+:"[ ]*/';
$removeEndJunkRegex = '/[";]+/'; /* Could use rtrim on each if wanted */

$matches = array();

/* Find all 's' bits */
preg_match_all($sSegmentsRegex, $banText, $matches); 
$matches = $matches[0]; /* preg_match_all changes $matches to array of arrays */

/* Remove start junk of each 's' bit */
$matches = preg_replace($removeStartJunkRegex, "", $matches); 
$matches = preg_replace($removeEndJunkRegex, "", $matches); 

foreach($matches as $ip) {
    if($_SERVER["REMOTE_ADDR"] == $ip) {
        die("Your IP is banned!");
    }
 }

print_r($matches); /* Shows the list of IP bans, remove this in your app */

示例:

如果文件是用JSON编码的,则需要运行JSON\u解码。你可以在生成的数组中循环现有的ban.txt看起来像疯子的输出。它很容易修复,但是为什么有些IP具有额外的间距,或者有些元素具有3个组件,而其他元素具有2个组件?(例如:s:Number:String vs i:Number vs a:Number:Object)?只是好奇为什么ips前面有空格?我不知道JSON编码。您能提供更多帮助吗,thanksIt看起来像是一个数组,但它似乎无效,而且它包含的值似乎也有点荒谬…警告:preg_match()[function.preg match]:在第9a:5:{i:13716;s:12:“127.0.1”;i:16731;s:14行的C:\Program Files\EasyPHP5.2.10\www\aa.php中,分隔符不能是字母数字或反斜杠“70.241.74.116”;i:2269;s:12:“1.52.251.63”;i:6048;s:15:“113.22.109.137”;i:9050;s:14:“123.21.100.174”}。这是一个输出,我尝试删除空格。你能帮我检查一下吗?修复了,尝试新代码(因为现在没有方便的PHP解释器,出现了几个错误)。警告:preg_match()[function.preg match]:第11A行的C:\Program Files\EasyPHP5.2.10\www\aa.php中的分隔符不能是字母数字或反斜杠有一个新错误:注意:第14行的C:\Program Files\EasyPHP5.2.10\www\aa.php中的数组到字符串转换。您好,我可能错了吗?
Regex for start junk (still need to trim end): s:[0-9]+:"[ ]*
Regex for end junk: [";]+
$banText = file_get_contents("ban.txt"); 

/* Evil, evil regexes */
$sSegmentsRegex = '/s:[0-9]+:"[ ]*[0-9]+.[0-9]+.[0-9]+.[0-9]+"/';
$removeStartJunkRegex = '/s:[0-9]+:"[ ]*/';
$removeEndJunkRegex = '/[";]+/'; /* Could use rtrim on each if wanted */

$matches = array();

/* Find all 's' bits */
preg_match_all($sSegmentsRegex, $banText, $matches); 
$matches = $matches[0]; /* preg_match_all changes $matches to array of arrays */

/* Remove start junk of each 's' bit */
$matches = preg_replace($removeStartJunkRegex, "", $matches); 
$matches = preg_replace($removeEndJunkRegex, "", $matches); 

foreach($matches as $ip) {
    if($_SERVER["REMOTE_ADDR"] == $ip) {
        die("Your IP is banned!");
    }
 }

print_r($matches); /* Shows the list of IP bans, remove this in your app */