How does PHP Disable Internet access?

Source: Internet
Author: User
If you disable Internet access to a PHP file, that is, this php file is only accessible to servers. Because a lot of data needs to be generated by regularly running PHP scripts on scheduled tasks to prevent the Internet or a spider from running this PHP file to generate incorrect data, therefore, you must block the Internet and only allow server access. If you disable Internet access to a PHP file, this PHP file is only accessible to servers.
Because a lot of data needs to be generated by regularly running php scripts on scheduled tasks to prevent the Internet or spider from running this PHP to generate incorrect data, you need to block the Internet and only access the server.

Reply content:

If you disable Internet access to a PHP file, this PHP file is only accessible to servers.
Because a lot of data needs to be generated by regularly running php scripts on scheduled tasks to prevent the Internet or spider from running this PHP to generate incorrect data, you need to block the Internet and only access the server.

Generally, the Web Server accepts HTTP requests and transfers them to mod_php or php-fpm to execute the PHP program. This means that the Web server can be fully restricted. The restriction Method for Apache server @ iefsou has been provided. I will write a restriction configuration for nginx:

location /uri/path/to/your/script.php {    allow 192.168.0.0/16;    allow 172.16.0.0/12;    allow 10.0.0.0/8;    allow 127.0.0.0/8;    deny all;}

This method can also be used to restrict access by individual IP addresses on the Internet. For details, refer to the nginx access module configuration manual.

Of course, it is not impossible to restrict the IP address in the PHP program. In the same way, determine whether the user's IP address is a lan ip Address:

function is_local_ip($ip_addr = null) {    if (is_null($ip_addr)) {        $ip_addr = $_SERVER['REMOTE_ADDR'];    }    $ip = ip2long($ip_addr);    return $ip & 0xffff0000 == 0xc0a80000 // 192.168.0.0/16        || $ip & 0xfff00000 == 0xac100000 // 172.16.0.0/12        || $ip & 0xff000000 == 0xa0000000 // 10.0.0.0/8        || $ip & 0xff000000 == 0x7f000000 // 127.0.0.0/8    ;}

If you are not from a LANheaderOutput 403 or an error page to show it.

It is worth mentioning that the PHP method will cause the request to be handed over to the PHP interpreter. However, the performance of the PHP interpreter is much worse than that of nginx to directly return 403, you can perform stress testing on your own. Of course, if you are developing a product that you have installed for the customer, do not change the server configurations of inexperienced customers.

Apache Url rewriting can solve this problem.

RewriteCond % {REQUEST_URI} myfile \. php [NC]
RewriteCond % {REMOTE_ADDR }! ^ 127.0.0.1 $
RewriteRule. *-[R = 503, L]

Http: // path/test. php/sign = 123456`
If ($ __get ["sign"]! = 123456) {exit ;}

Match the obtained IP address with the Intranet IP Address

I feel that it is more convenient to find another port in the gateway and put it on that port.

The full name of the Robots Protocol (also known as crawler Protocol and robot Protocol) is "Robots Exclusion Protocol)

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.