我制作的 PHP 拦截器无法正常工作
My made PHP Blocker not working
此代码有效:
https://www.skools.cu.ma/contents/posts/Comma.php:
<?php
require ('./../../blocks/block_function.php');
?>
https://www.skools.cu.ma/blocks/block_function.php:
<?php
$badcomment_comma_post = array("137.97.11.13", "222.222.222", "333.333.333");
if ( in_array ($_SERVER['REMOTE_ADDR'], $badcomment_comma_post) ) {
die("<script>window.location = 'https://www.skools.cu.ma/blocks/bad_comment.php';</script>You have been blocked");
}
?>
但是当我尝试分离 IP 地址和重定向器代码时,它不起作用并且我没有收到 PHP 错误消息:
https://www.skools.cu.ma/blocks/block_function.php:
<?php
$file = file_get_contents('./../../blocks/bad_commented_ip.txt');
$badcomment_comma_post = array($file);
if ( in_array ($_SERVER['REMOTE_ADDR'], $badcomment_comma_post) ) {
die("<script>window.location = 'https://www.skools.cu.ma/blocks/bad_comment.php';</script>You have been blocked");
}
?>
https://www.skools.cu.ma/blocks/bad_commented_ip.txt:
"137.97.11.13", "222.222.222", "333.333.333"
请帮忙?
您 运行 遇到的问题是:
$file = file_get_contents('./../../blocks/bad_commented_ip.txt');
$badcomment_comma_post = array($file);
实际上(如果你print_r($badcomment_comma_post);
)正在做的是一个数组元素的值为整个文件.每个IP不是多个数组元素:
Array
(
[0] => "137.97.11.13", "222.222.222", "333.333.333"
)
如果你的文件实际上是"137.97.11.13", "222.222.222", "333.333.333"
的格式,那么考虑下面的例子来做:
// Example 1, explode!
$file = file_get_contents('./../../blocks/bad_commented_ip.txt');
$cleaned = str_replace(array('"',' '),'',$file);
$badcomment_comma_post = explode(',',$cleaned);
它所做的是读入文件,去除所有引号和空格,然后用逗号展开以生成您想要的数组。
另一种方法是在文件中使用 json(但您的文件内容不完全正确):
// Example 2, json!
$file = file_get_contents('./../../blocks/bad_commented_ip.txt');
$json = '['. $file .']';
$badcomment_comma_post = json_decode($json);
那么它的作用是获取文件内容,将它们用方括号括起来,以便更好地使用 json 格式,然后最终将 json 数组解码为 php数组。
以上两个例子会输出print_r($badcomment_comma_post);
:
Array
(
[0] => 137.97.11.13
[1] => 222.222.222
[2] => 333.333.333
)
此代码有效:
https://www.skools.cu.ma/contents/posts/Comma.php:
<?php
require ('./../../blocks/block_function.php');
?>
https://www.skools.cu.ma/blocks/block_function.php:
<?php
$badcomment_comma_post = array("137.97.11.13", "222.222.222", "333.333.333");
if ( in_array ($_SERVER['REMOTE_ADDR'], $badcomment_comma_post) ) {
die("<script>window.location = 'https://www.skools.cu.ma/blocks/bad_comment.php';</script>You have been blocked");
}
?>
但是当我尝试分离 IP 地址和重定向器代码时,它不起作用并且我没有收到 PHP 错误消息:
https://www.skools.cu.ma/blocks/block_function.php:
<?php
$file = file_get_contents('./../../blocks/bad_commented_ip.txt');
$badcomment_comma_post = array($file);
if ( in_array ($_SERVER['REMOTE_ADDR'], $badcomment_comma_post) ) {
die("<script>window.location = 'https://www.skools.cu.ma/blocks/bad_comment.php';</script>You have been blocked");
}
?>
https://www.skools.cu.ma/blocks/bad_commented_ip.txt:
"137.97.11.13", "222.222.222", "333.333.333"
请帮忙?
您 运行 遇到的问题是:
$file = file_get_contents('./../../blocks/bad_commented_ip.txt');
$badcomment_comma_post = array($file);
实际上(如果你print_r($badcomment_comma_post);
)正在做的是一个数组元素的值为整个文件.每个IP不是多个数组元素:
Array
(
[0] => "137.97.11.13", "222.222.222", "333.333.333"
)
如果你的文件实际上是"137.97.11.13", "222.222.222", "333.333.333"
的格式,那么考虑下面的例子来做:
// Example 1, explode!
$file = file_get_contents('./../../blocks/bad_commented_ip.txt');
$cleaned = str_replace(array('"',' '),'',$file);
$badcomment_comma_post = explode(',',$cleaned);
它所做的是读入文件,去除所有引号和空格,然后用逗号展开以生成您想要的数组。
另一种方法是在文件中使用 json(但您的文件内容不完全正确):
// Example 2, json!
$file = file_get_contents('./../../blocks/bad_commented_ip.txt');
$json = '['. $file .']';
$badcomment_comma_post = json_decode($json);
那么它的作用是获取文件内容,将它们用方括号括起来,以便更好地使用 json 格式,然后最终将 json 数组解码为 php数组。
以上两个例子会输出print_r($badcomment_comma_post);
:
Array
(
[0] => 137.97.11.13
[1] => 222.222.222
[2] => 333.333.333
)