I’m trying to redirect all traffic from outside of the local network to a page like 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.

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:

<?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.

其他提示

You may try:

$ip_array = explode(".", $_SERVER['REMOTE_ADDR']);
if($ip_array[0]!="192") 
    redirect(noallowed.php); 

Or substr() version:

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归因
不隶属于 StackOverflow
scroll top