{"id":1529,"date":"2013-01-23T09:54:15","date_gmt":"2013-01-23T09:54:15","guid":{"rendered":"http:\/\/webexplorar.com\/?p=1529"},"modified":"2023-03-27T15:19:38","modified_gmt":"2023-03-27T15:19:38","slug":"php-ip-address-based-redirect-example","status":"publish","type":"post","link":"https:\/\/webexplorar.com\/php-ip-address-based-redirect-example\/","title":{"rendered":"PHP IP address based redirect example"},"content":{"rendered":"<p>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.<br \/>\n<!--more--><\/p>\n<h2 style=\"color: #093;\">Step-1<\/h2>\n<p>You can put IP addresses as follows in the text file named &#8220;<strong>ip-address.txt<\/strong>&#8221; file.<br \/>\n<code><br \/>\n127.2.0.1\/27<br \/>\n127.3.0.1\/32<br \/>\n127.0.0.1<br \/>\n<\/code><\/p>\n<h2 style=\"color: #093;\">Step-2<\/h2>\n<p>Now get this IP addresses line by line.You can get more info about reading file line by line in my another post named &#8220;<a title=\"PHP read file line by line to an array\" href=\"http:\/\/webexplorar.com\/php-read-file-line-by-line-to-an-array\" target=\"_blank\" rel=\"noopener noreferrer\">PHP read file line by line to an array<\/a>&#8220;.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$ipAddresses= 'ip-addresses.txt';\r\n$cidrs = file($ipAddresses, FILE_IGNORE_NEW_LINES);<\/pre>\n<p>&nbsp;<\/p>\n<h2 style=\"color: #093;\">Step-3<\/h2>\n<p>Get visitor IP address.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$user_ip = $_SERVER['REMOTE_ADDR']; \/\/ get user ip<\/pre>\n<p>&nbsp;<\/p>\n<h2 style=\"color: #093;\">Step-4<\/h2>\n<p>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.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">    \r\nfunction checkUserIP($user_ip, $cidrs) {\r\n    $ipu = explode('.', $user_ip);\r\n    foreach ($ipu as =&gt; $v)\r\n    $v = str_pad(decbin($v), 8, '0', STR_PAD_LEFT);\r\n    $ipu = join('', $ipu);\r\n    $res = false;\r\n    foreach ($cidrs as $cidr) {\r\n        $parts = explode('\/', $cidr);\r\n        $ipc = explode('.', $parts[0]);\r\n        foreach ($ipc as =&gt; $v)\r\n          $v = str_pad(decbin($v), 8, '0', STR_PAD_LEFT);\r\n          $ipc = substr(join('', $ipc), 0, $parts[1]);\r\n          $ipux = substr($ipu, 0, $parts[1]);\r\n          $res = ($ipc === $ipux);\r\n         if ($res) break;\r\n    }\r\n  return $res;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h2 style=\"color: #093;\">Step-5<\/h2>\n<p>Finally get response from above function and redirect them.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">\/\/ checking user ip address redirect \r\nif (checkUserIP($user_ip, $cidrs)) { \r\n    echo 'IP Address is - '.$user_ip.' Your ip is checked and that is exist in the black list.'; \r\n} else { \r\n   echo 'Access denied this ip-'.$user_ip; \r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h2 style=\"color: #0166fe;\">Putting all together<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">function checkUserIP($user_ip, $cidrs) {\r\n    $ipu = explode('.', $user_ip);\r\n    foreach ($ipu as =&gt; $v)\r\n    $v = str_pad(decbin($v), 8, '0', STR_PAD_LEFT);\r\n    $ipu = join('', $ipu);\r\n    $res = false;\r\n    foreach ($cidrs as $cidr) {\r\n        $parts = explode('\/', $cidr);\r\n        $ipc = explode('.', $parts[0]);\r\n        foreach ($ipc as =&gt; $v)\r\n        $v = str_pad(decbin($v), 8, '0', STR_PAD_LEFT);\r\n        $ipc = substr(join('', $ipc), 0, $parts[1]);\r\n        $ipux = substr($ipu, 0, $parts[1]);\r\n        $res = ($ipc === $ipux);\r\n        if ($res) break;\r\n    }\r\n  return $res;\r\n}\r\n\/\/ Read file\r\n$filename = 'ip-addresses.txt';\r\n$cidrs = file($filename, FILE_IGNORE_NEW_LINES);\r\n$user_ip = $_SERVER['REMOTE_ADDR']; \/\/ get user ip\r\n\/\/ checking user ip address redirect\r\nif (checkUserIP($user_ip, $cidrs)) {\r\n   echo 'IP Address is - '.$user_ip.' Your ip is checked and that is exist in the black list.';\r\n}\r\nelse {\r\n   echo 'Access denied this ip-'.$user_ip;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Check a particular network\/mask combination.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$network = ip2long(\"192.168.105.12\");\r\n$mask    = ip2long(\"255.255.255.0\"); \/\/ subnet mask\r\n$remote  = ip2long($_SERVER['REMOTE_ADDR']); \/\/ visitor IP address\r\nif (($remote &amp; $mask)==$network)\r\n{\r\n    header(\"Location: http:\/\/webexplorar.com\");\r\n    exit;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Note:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">echo $_SERVER['REMOTE_ADDR'];  \/\/ip of visitor - you need this\r\necho $_SERVER['SERVER_ADDR'];  \/\/ip of server<\/pre>\n<p>&nbsp;<\/p>\n\n<a  data-e-Disable-Page-Transition=\"true\" class=\"download-link download-button aligncenter\" title=\"Version 1.0\" href=\"https:\/\/webexplorar.com\/download\/3108\/?tmstv=1776795846\" rel=\"nofollow\" id=\"download-link-3108\" data-redirect=\"false\" >\n\tDownload &ldquo;PHP IP address based redirect example&rdquo;\t<small>PHP-ip-address-based-redirect-example.zip\t\t&ndash; Downloaded 1023 times\t\t&ndash; 1.21 KB<\/small>\n<\/a>\n\n","protected":false},"excerpt":{"rendered":"<p>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.<\/p>\n","protected":false},"author":1,"featured_media":1534,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121,1],"tags":[],"class_list":["post-1529","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","category-recent-posts","post--single"],"aioseo_notices":[],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/webexplorar.com\/wp-json\/wp\/v2\/posts\/1529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webexplorar.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webexplorar.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webexplorar.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webexplorar.com\/wp-json\/wp\/v2\/comments?post=1529"}],"version-history":[{"count":1,"href":"https:\/\/webexplorar.com\/wp-json\/wp\/v2\/posts\/1529\/revisions"}],"predecessor-version":[{"id":3748,"href":"https:\/\/webexplorar.com\/wp-json\/wp\/v2\/posts\/1529\/revisions\/3748"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webexplorar.com\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/webexplorar.com\/wp-json\/wp\/v2\/media?parent=1529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webexplorar.com\/wp-json\/wp\/v2\/categories?post=1529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webexplorar.com\/wp-json\/wp\/v2\/tags?post=1529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}