-
-
- /**
- * Check whether the accessed IP is a specified allowed IP
- * Enter description here...
- */
- function check_ip(){
- $ALLOWED_IP=array('192.168.2.*','127.0.0.1','192.168 .2.49');
- $IP=getIP();
- $check_ip_arr= explode('.',$IP);//Split the IP to be detected into an array
- #Restrict IP
- if(!in_array($IP, $ALLOWED_IP)) {
- foreach ($ALLOWED_IP as $val){
- if(strpos($val,'*')!==false){//Found * substitution symbol
- $arr=array();/ /
- $arr=explode('.', $val);
- $bl=true;//Used to record whether there is a successful match in loop detection
- for($i=0;$i<4;$i++) {
- if($arr[$i]!='*'){//If it is not equal to *, it will be checked. If it is a * symbol replacement, it will not be checked
- if($arr[$i]!=$check_ip_arr[ $i]){
- $bl=false;
- break;//Stop checking this ip and continue checking the next ip
- }
- }
- }//end for
- if($bl){//If it is true, find it If there is a successful match, return
- return;
- die;
- }
- }
- }//end foreach
- header('HTTP/1.1 403 Forbidden');
- echo "Access forbidden";
- die;
- }
- } < ;/p>
/**
- * Obtain accessed IP
- * 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"]);
- }
-
Copy code
Call method:
In the required files, add the call check_ip(); to achieve the purpose of IP access restriction;
This function allows only the specified IP to access the file, and provides the wildcard character * in the IP to match multiple IPs.
|