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, you can refer to the Core Function Code 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; // stop checking this ip address and continue checking the next ip address }}// end for if ($ 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.