Today, a friend asked me how to allow access from only the specified IP address and prohibit access from other ip addresses by default. what I previously shared in my website is how to restrict access from the specified ip address, 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.