A summary of code that prohibits single IP and IP segment access in PHP

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.