Code that restricts IP segment access in PHP and prevents IP from submitting forms

Source: Internet
Author: User

Add the following code to the page where you want to prohibit access or submit a form.

Note: Below is just an instance code that restricts IP to PHP, and if you intend to apply it to a CMS, modify it yourself.

<?PHP/Add 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 ']; } //Limit IPif($userip= = ' 192.168.1.88 '){ Header("location:http://t.qq.com/wb631992791");//skip to Weibo after being bannedExit; } //restricting IP segments$ip _arr=Explode(‘.‘,$userip); #limit the IP segment, assuming it is 192.168.*.*if(! (($ip _arr[0] = = ' 192 ' &&$ip _arr[1]== ' 168 ') )){ Header("location:http://t.qq.com/wb631992791");//skip to Weibo after being bannedExit; }Else{ Header("location:http://afish.cnblogs.com");//normal IP Direct access to the small fish Court homeExit; } ?>

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.