PHP only allows access from specified IP addresses (allow * wildcard characters to filter IP addresses)

Source: Internet
Author: User
Today, a friend asked me how to allow access from only specified IP addresses. By default, access from other ip addresses is prohibited. In the past, I shared how to restrict access from specified ip addresses, for more information, see

Today, a friend asked me how to allow access from only specified IP addresses. By default, access from other ip addresses is prohibited. In the past, I shared how to restrict access from specified ip addresses, for more information, see

The Core Function Code is as follows:

/*** Check whether the accessed ip address is a specified allowed ip Address * Enter description here... */function check_ip () {$ ALLOWED_IP = array ('2017. 168. 2. * ', '2017. 0.0.1 ', '2017. 168.2.49 '); $ IP = getIP (); $ check_ip_arr = explode ('. ', $ IP); // split the ip address to be detected into an array # Restrict IPif (! In_array ($ IP, $ ALLOWED_IP) {foreach ($ ALLOWED_IP as $ val) {if (strpos ($ val ,'*')! = False) {// The * Replacement character $ arr = array (); // $ arr = explode ('. ', $ val); $ bl = true; // used to record whether a matched for ($ I = 0; $ I <4; $ I ++) {if ($ arr [$ I]! = '*') {// Not equal to *. Check if ($ arr [$ I]) is not checked if it is a * symbol replacement. = $ Check_ip_arr [$ I]) {$ bl = false; break; // terminate this ip address and continue to check the next ip address.} // end forif ($ bl) {// if it is true, return the return; die ;}}// end foreachheader ('HTTP/1.1 403 Forbidden ') if a successful match is found '); echo "Access forbidden"; die;} * obtain the accessed IP Address * Enter description here... */function getIP () {return isset ($ _ SERVER ["HTTP_X_FORWARDED_FOR"])? $ _ SERVER ["HTTP_X_FORWARDED_FOR"] :( isset ($ _ SERVER ["HTTP_CLIENT_IP"])? $ _ SERVER ["HTTP_CLIENT_IP"]: $ _ SERVER ["REMOTE_ADDR"]);}

Add the check_ip (); To be detected. This function allows only the specified IP address to access the file, and provides the * wildcard in the IP address to match multiple IP addresses.

,

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.