在txt内容php中查找ip

发布于 2024-11-04 20:32:11 字数 460 浏览 0 评论 0原文

我有一个文本文件: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深缘浅 2024-11-11 20:32:11

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

正则表达式似乎从来都不是一个很好的解决方案,但我没有关于文件一致性的详细信息。

1.在你的 ban.txt 中隔离“s”段

因此,我的正则表达式并不出色,但是这个正则表达式应该与似乎用于 IP 禁令的“s”段相匹配(尽管您的评论指出“IP总是在“ip”中”这有点令人困惑)。

Regex: s:[0-9]+:"[ ]*[0-9]+.[0-9]+.[0-9]+.[0-9]+";

2.隔离每个“s”段内的 IP

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

Regex for start junk (still need to trim end): s:[0-9]+:"[ ]*
Regex for end junk: [";]+

3。示例代码

这将为我们提供以下 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 */

示例: 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).

Regex: s:[0-9]+:"[ ]*[0-9]+.[0-9]+.[0-9]+.[0-9]+";

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"; into 192.168.0.0";), and afterwards trim the end quotation mark and semi-colon (i.e. 192.168.0.0"; to 192.168.0.0):

Regex for start junk (still need to trim end): s:[0-9]+:"[ ]*
Regex for end junk: [";]+

3. Example Code

This would give us this PHP code:

$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 */

Example: http://codepad.viper-7.com/S9rTQe

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文