{"id":169,"date":"2010-12-06T11:33:05","date_gmt":"2010-12-06T11:33:05","guid":{"rendered":"http:\/\/www.osd.net\/blog\/?p=169"},"modified":"2010-12-09T13:09:22","modified_gmt":"2010-12-09T13:09:22","slug":"simple-script-to-ban-an-ip-address","status":"publish","type":"post","link":"https:\/\/www.osd.net\/blog\/web-development\/php\/simple-script-to-ban-an-ip-address\/","title":{"rendered":"Simple script to ban an IP address"},"content":{"rendered":"<p>Below you can find a simple php script that blocks ip addresses. It uses a function (called <strong>iplong<\/strong>) that converts the standard IPv4 address to a long int value and the main function (<strong>ban<\/strong>) that checks if a provided ip can be found in a list of blocked addresses. Hope you will find this useful.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction ban($range = array(), $ip = '')\r\n{\r\n        $ip = longip(trim($ip));\r\n        if($ip == FALSE)\r\n        {\r\n                return FALSE;\r\n        }\r\n        if(empty($ip))\r\n        {\r\n                return FALSE;\r\n        }\r\n        foreach($range AS $key =&gt; $val)\r\n        {\r\n                $temp = explode('-', $val);\r\n                if(empty($temp[0]))\r\n                {\r\n                        return FALSE;\r\n                }\r\n                else\r\n                {\r\n                        $start_ip = longip(trim($temp[0]));\r\n                        if($start_ip == FALSE)\r\n                        {\r\n                                return FALSE;\r\n                        }\r\n                }\r\n                if(empty($temp[1]))\r\n                {\r\n                        if($ip == $start_ip)\r\n                        {\r\n                                return TRUE;\r\n                        }\r\n                }\r\n                else\r\n                {\r\n                        $stop_ip = longip(trim($temp[1]));\r\n                        if($stop_ip == FALSE)\r\n                        {\r\n                                return FALSE;\r\n                        }\r\n                }\r\n                if($start_ip &lt;= $ip &amp;&amp; $ip &lt;= $stop_ip)\r\n                {\r\n                        return TRUE;\r\n                }\r\n        }\r\n        return FALSE;\r\n}\r\nfunction longip($ip)\r\n{\r\n        if(empty($ip))\r\n        {\r\n                return FALSE;\r\n        }\r\n        $block = explode('.', $ip);\r\n        if(count($block) != 4)\r\n        {\r\n                return FALSE;\r\n        }\r\n        $i = 3;\r\n        $block_ip = 0;\r\n        foreach($block as $k =&gt; $v)\r\n        {\r\n                $v = filter_var($v, FILTER_VALIDATE_INT);\r\n                if($v &lt; 0)\r\n                {\r\n                        $v = 0;\r\n                }\r\n                if($v &gt; 255)\r\n                {\r\n                        $v = 255;\r\n                }\r\n                $block_ip += pow(256, $i)*$v;\r\n                $i--;\r\n        }\r\n        return $block_ip;\r\n}\r\n\r\n\/\/usage\r\n$block_range = array(   '192.168.1.0 - 192.168.2.255',\r\n                        '192.168.4.0 - 192.168.4.255',\r\n                        '192.168.6.1'\r\n                );\r\n\r\n$ip = '192.168.3.255';\r\n\r\nif(ban($block_range, '192.168.6.1'))\r\n{\r\n        echo &quot;BAN\\n&quot;;\r\n}\r\nelse\r\n{\r\n        echo &quot;UNBAN\\n&quot;;\r\n}\r\n<\/pre>\n<div id=\"share-and-beer-container\">\t<div id=\"buy_me_a_beer_div\" class=\"single beer\">\n\t \t\n\t\t<div class=\"buy-beer\" onclick=\"document.getElementById('buy_me_a_beer_form').submit();\">\n\t\t\t<form action=\"https:\/\/www.paypal.com\/cgi-bin\/webscr\" id=\"buy_me_a_beer_form\" method=\"post\">\n\t\t\t\t<input type=\"hidden\" name=\"cmd\" value=\"_xclick\" \/>\n\t\t\t\t<input type=\"hidden\" name=\"business\" value=\"info@directaccess.ro\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"item_name\" value=\"A Beer For Simple script to ban an IP address\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"item_number\" value=\"1\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"return\" value=\"https:\/\/www.osd.net\/blog\/web-development\/php\/simple-script-to-ban-an-ip-address\/\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"amount\" value=\"5\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"undefined_quantity\" value=\"1\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"currency_code\" value=\"USD\" \/>  \n\t\t\t<\/form>\n\t\t\t<p class=\"buy-beer-text\">If you liked this post <br \/> you can <strong>buy me a beer<\/strong><\/p>\n\t\t<\/div>\n\t<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Below you can find a simple php script that blocks ip addresses. It uses a function (called iplong) that converts the standard IPv4 address to a long int value and the main function (ban) that checks if a provided ip can be found in a list of blocked addresses. Hope you will find this useful. &hellip;<\/p>\n<div class=\"cta1\"><a href=\"https:\/\/www.osd.net\/blog\/web-development\/php\/simple-script-to-ban-an-ip-address\/\">Read more<\/a><\/div>\n<div class=\"like-excerpt\"><div\n        class=\"fb-like\"\n        data-href=\"https:\/\/www.osd.net\/blog\/web-development\/php\/simple-script-to-ban-an-ip-address\/\"\n        data-layout=\"button_count\"\n        data-action=\"like\"\n        data-show-faces=\"false\"\n        data-share=\"false\">\n        <\/div><\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[14],"tags":[45,46,44,35],"_links":{"self":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/169"}],"collection":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/comments?post=169"}],"version-history":[{"count":13,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/169\/revisions"}],"predecessor-version":[{"id":206,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/169\/revisions\/206"}],"wp:attachment":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/media?parent=169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/categories?post=169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/tags?post=169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}