Skip to content Skip to sidebar Skip to footer
Reading Time: 2 minutes

This is a IP address redirect example.Check visitor IP address is exist in the IP address list/black list and if exist then visitor redirect to the another location using php.You can put your IP addresses list as line by line with single IP address or CIDR notation.Then check visitor IP address with list and redirect them.

Step-1

You can put IP addresses as follows in the text file named “ip-address.txt” file.

127.2.0.1/27
127.3.0.1/32
127.0.0.1

Step-2

Now get this IP addresses line by line.You can get more info about reading file line by line in my another post named “PHP read file line by line to an array“.

$ipAddresses= 'ip-addresses.txt';
$cidrs = file($ipAddresses, FILE_IGNORE_NEW_LINES);

 

Step-3

Get visitor IP address.

$user_ip = $_SERVER['REMOTE_ADDR']; // get user ip

 

Step-4

Then you need to pass visitor IP address and read IP addresses list as parameters to following function.It will return IP address is exist or not.

    
function checkUserIP($user_ip, $cidrs) {
    $ipu = explode('.', $user_ip);
    foreach ($ipu as => $v)
    $v = str_pad(decbin($v), 8, '0', STR_PAD_LEFT);
    $ipu = join('', $ipu);
    $res = false;
    foreach ($cidrs as $cidr) {
        $parts = explode('/', $cidr);
        $ipc = explode('.', $parts[0]);
        foreach ($ipc as => $v)
          $v = str_pad(decbin($v), 8, '0', STR_PAD_LEFT);
          $ipc = substr(join('', $ipc), 0, $parts[1]);
          $ipux = substr($ipu, 0, $parts[1]);
          $res = ($ipc === $ipux);
         if ($res) break;
    }
  return $res;
}

 

Step-5

Finally get response from above function and redirect them.

// checking user ip address redirect 
if (checkUserIP($user_ip, $cidrs)) { 
    echo 'IP Address is - '.$user_ip.' Your ip is checked and that is exist in the black list.'; 
} else { 
   echo 'Access denied this ip-'.$user_ip; 
}

 

 

Putting all together

function checkUserIP($user_ip, $cidrs) {
    $ipu = explode('.', $user_ip);
    foreach ($ipu as => $v)
    $v = str_pad(decbin($v), 8, '0', STR_PAD_LEFT);
    $ipu = join('', $ipu);
    $res = false;
    foreach ($cidrs as $cidr) {
        $parts = explode('/', $cidr);
        $ipc = explode('.', $parts[0]);
        foreach ($ipc as => $v)
        $v = str_pad(decbin($v), 8, '0', STR_PAD_LEFT);
        $ipc = substr(join('', $ipc), 0, $parts[1]);
        $ipux = substr($ipu, 0, $parts[1]);
        $res = ($ipc === $ipux);
        if ($res) break;
    }
  return $res;
}
// Read file
$filename = 'ip-addresses.txt';
$cidrs = file($filename, FILE_IGNORE_NEW_LINES);
$user_ip = $_SERVER['REMOTE_ADDR']; // get user ip
// checking user ip address redirect
if (checkUserIP($user_ip, $cidrs)) {
   echo 'IP Address is - '.$user_ip.' Your ip is checked and that is exist in the black list.';
}
else {
   echo 'Access denied this ip-'.$user_ip;
}

 

Check a particular network/mask combination.

$network = ip2long("192.168.105.12");
$mask    = ip2long("255.255.255.0"); // subnet mask
$remote  = ip2long($_SERVER['REMOTE_ADDR']); // visitor IP address
if (($remote & $mask)==$network)
{
    header("Location: http://webexplorar.com");
    exit;
}

 

Note:

echo $_SERVER['REMOTE_ADDR'];  //ip of visitor - you need this
echo $_SERVER['SERVER_ADDR'];  //ip of server

 

Download “PHP IP address based redirect example”

PHP-ip-address-based-redirect-example.zip – Downloaded 447 times – 1.21 KB