在txt内容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($banlist as $ips ) {
if($_SERVER["REMOTE_ADDR"] == $ips) {
die("Your IP is banned!");
}
}
?>
可以帮助我列出此内容中的IP,我是php新手。非常感谢
I have a text file : ban.txt have content
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";}
I write a script to find and ban IP in this txt
<?php
$banlist = file("ban.txt");
foreach($banlist as $ips ) {
if($_SERVER["REMOTE_ADDR"] == $ips) {
die("Your IP is banned!");
}
}
?>
Can help me to list IP in this content, i m a newbie php. Thanks very much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看这是一个基于不清楚问题的公认的垃圾解决方案
正则表达式似乎从来都不是一个很好的解决方案,但我没有关于文件一致性的详细信息。
1.在你的 ban.txt 中隔离“s”段
因此,我的正则表达式并不出色,但是这个正则表达式应该与似乎用于 IP 禁令的“s”段相匹配(尽管您的评论指出“IP总是在“ip”中”这有点令人困惑)。
2.隔离每个“s”段内的 IP
一旦我们有了这些段,我们就可以将起始位剥离为实际 IP(即转
s:123:"192.168.0.0";
成192.168.0.0";
),然后修剪结束引号和分号(即192.168.0.0";
为192.168.0.0
):3。示例代码
这将为我们提供以下 PHP 代码:
示例: http://codepad.viper-7 .com/S9rTQe
Look this is an acknowledged crap solution based on an unclear question
Regex never seems a great solution, but I don't have a lot of detail on how consistent the file is.
1. Isolate "s" segments in your ban.txt
As such, and my regex isn't fantastic, but this regex should match the "s" segments which appear to be for IP bans (although your comment stating "The IP always in "ip"" confuses this a little).
2. Isolate the IPs within each "s" segment
Once we have these segments, we can strip the start bit up to the actual IP (i.e. turn
s:123:"192.168.0.0";
into192.168.0.0";
), and afterwards trim the end quotation mark and semi-colon (i.e.192.168.0.0";
to192.168.0.0
):3. Example Code
This would give us this PHP code:
Example: http://codepad.viper-7.com/S9rTQe