1. Prohibit single IP
<?PHP//IP Access Restrictionsif(getenv(' Http_client_ip ') &&strcasecmp(getenv(' Http_client_ip '), ' unknown ')) {$userip=getenv(' Http_client_ip ');} ElseIf(getenv(' Http_x_forwarded_for ') &&strcasecmp(getenv(' http_x_forwarded_for '), ' unknown ')) {$userip=getenv(' Http_x_forwarded_for ');} ElseIf(getenv(' REMOTE_ADDR ') &&strcasecmp(getenv(' REMOTE_ADDR '), ' unknown ')) {$userip=getenv(' REMOTE_ADDR ');} ElseIf(isset($_server[' REMOTE_ADDR ']) &&$_server[' REMOTE_ADDR '] &&strcasecmp($_server[' REMOTE_ADDR '], ' unknown ')) {$userip=$_server[' REMOTE_ADDR '];}$banned _ip=Array ("127.0.0.1", "203.0.0.1", "56.12.50.65", "192.168.1.88");if(In_array($userip,$banned _ip)) { die("Your IP is a block to connect!"));}Echo"Small Fish Court test";?>
2. Prohibit IP segment
<?PHP//Determine IP access Restrictionsif(getenv(' Http_client_ip ') &&strcasecmp(getenv(' Http_client_ip '), ' unknown ')) { $userip=getenv(' Http_client_ip '); } ElseIf(getenv(' Http_x_forwarded_for ') &&strcasecmp(getenv(' http_x_forwarded_for '), ' unknown ')) { $userip=getenv(' Http_x_forwarded_for '); } ElseIf(getenv(' REMOTE_ADDR ') &&strcasecmp(getenv(' REMOTE_ADDR '), ' unknown ')) { $userip=getenv(' REMOTE_ADDR '); } ElseIf(isset($_server[' REMOTE_ADDR ']) &&$_server[' REMOTE_ADDR '] &&strcasecmp($_server[' REMOTE_ADDR '], ' unknown ')) { $userip=$_server[' REMOTE_ADDR ']; } $ban _range_low=Ip2long("217.0.0.0");//IP Segment Header$ban _range_up=Ip2long("217.255.255.255");//IP Segment End$ip=Ip2long($userip]); if($ip>$ban _range_low&&$ip<$ban _range_up) { Print"Banned"; Exit(); } ?>
Also, please note that PHP Ip2long has a bug, please use caution
<? echoip2long(' 58.99.11.1 '), "<br/>"; // echoip2long(' 58.99.011.1 '), "<br/>"; // echoip2long(' 058.99.11.1 '), "<br/>"; //
In PHP 4.x,php 5.x, the result of an IP conversion with a leading zero is incorrect.
Workaround, use your own function:
function myip2long ($ip) { $ip _arrsplit(' \. ',$ip); $iplong intval ($ip _arrintval($ip _arrintval($ip _arrintval( $ip _arr [3]); return $iplong ; }
Complete!