[英]How to allow local IP addresses range and redirect if not using PHP instead of .htaccess
I'm trying to redirect all traffic from outside of the local network to a page like notallowed.php . 我正在尝试将所有流量从本地网络外部重定向到诸如notallowed.php之类的页面。
There's no possibility of using .htaccess so I'm trying to do this with PHP, but I'm not sure how I can use a wildcard function on all addresses that begin with 192
. 无法使用.htaccess,因此我尝试使用PHP进行此操作,但不确定如何在以
192
开头的所有地址上使用通配符函数。
I started out with something like: 我开始的时候是这样的:
if ($_SERVER['REMOTE_ADDR'] != "192.*.*.*") {
redirect('noallowed.php');
}
What would be the most efficient way to do this? 什么是最有效的方法?
You would need to use a regular expression to check the string begins with 192
: 您将需要使用正则表达式来检查以
192
开头的字符串:
<?php
if (!preg_match('/^192/', $_SERVER['REMOTE_ADDR'])) {
header('HTTP/1.1 403 Forbidden');
header('Location: notallowed.php');
exit;
}
I'm also sending a 403
status code to be RESTful. 我还发送了
403
状态代码以实现RESTful。
You may try: 您可以尝试:
$ip_array = explode(".", $_SERVER['REMOTE_ADDR']);
if($ip_array[0]!="192")
redirect(noallowed.php);
Or substr() version: 或substr()版本:
if(substr($_SERVER['REMOTE_ADDR'],0,3)!="192")
redirect(noallowed.php);
if(!isset($_SERVER['REMOTE_ADDR']) ||
(isset($_SERVER['REMOTE_ADDR']) && strpos($_SERVER['REMOTE_ADDR'], '192.') !== 0)) {
header('Location: /notallowed.php');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.