search
Homephp教程php手册PHP限制IP访问 只允许指定IP访问 允许*号通配符过滤IP

PHP限制IP访问 只允许指定IP访问 允许*号通配符过滤IP

Jun 13, 2016 am 10:54 AM
DEenterphpfordesignationDetectionofaccessfilterWildcardlimit

[php]

01./**
02. * 检测访问的ip是否为规定的允许的ip
03. * Enter description here ...
04. */ 
05.function check_ip(){ 
06.    $ALLOWED_IP=array('192.168.2.*','127.0.0.1','192.168.2.49'); 
07.    $IP=getIP(); 
08.    $check_ip_arr= explode('.',$IP);//要检测的ip拆分成数组  
09.    #限制IP 
10.    if(!in_array($IP,$ALLOWED_IP)) { 
11.        foreach ($ALLOWED_IP as $val){ 
12.            if(strpos($val,'*')!==false){//发现有*号替代符  
13.                 $arr=array();//  
14.                 $arr=explode('.', $val); 
15.                 $bl=true;//用于记录循环检测中是否有匹配成功的  
16.                 for($i=0;$i 17.                    if($arr[$i]!='*'){//不等于*  就要进来检测,如果为*符号替代符就不检查  
18.                        if($arr[$i]!=$check_ip_arr[$i]){ 
19.                            $bl=false; 
20.                            break;//终止检查本个ip 继续检查下一个ip  
21.                        } 
22.                    } 
23.                 }//end for   
24.                 if($bl){//如果是true则找到有一个匹配成功的就返回  
25.                    return; 
26.                    die; 
27.                 } 
28.            } 
29.        }//end foreach  
30.        header('HTTP/1.1 403 Forbidden'); 
31.        echo "Access forbidden"; 
32.        die; 
33.    } 
34.} 
/**
 * 检测访问的ip是否为规定的允许的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);//要检测的ip拆分成数组
 #限制IP
 if(!in_array($IP,$ALLOWED_IP)) {
  foreach ($ALLOWED_IP as $val){
      if(strpos($val,'*')!==false){//发现有*号替代符
        $arr=array();//
        $arr=explode('.', $val);
        $bl=true;//用于记录循环检测中是否有匹配成功的
        for($i=0;$i          if($arr[$i]!='*'){//不等于*  就要进来检测,如果为*符号替代符就不检查
          if($arr[$i]!=$check_ip_arr[$i]){
           $bl=false;
           break;//终止检查本个ip 继续检查下一个ip
          }
         }
        }//end for
        if($bl){//如果是true则找到有一个匹配成功的就返回
         return;
         die;
        }
      }
  }//end foreach
  header('HTTP/1.1 403 Forbidden');
  echo "Access forbidden";
  die;
 }
}[php] 
01.  
 [php] 
01.

/**<br />
02. * 获得访问的IP<br />
03. * Enter description here ...<br />
04. */ <br />
05.function getIP() { <br />
06.    return isset($_SERVER["HTTP_X_FORWARDED_FOR"])?$_SERVER["HTTP_X_FORWARDED_FOR"] <br />
07.    :(isset($_SERVER["HTTP_CLIENT_IP"])?$_SERVER["HTTP_CLIENT_IP"] <br />
08.    :$_SERVER["REMOTE_ADDR"]); <br />
09.}

 
10.

 
11.
 
12.在需要检测的地方 加上调用 check_ip(); 即可;  本函数提供只允许指定的IP访问文件,并提供IP中*号通配符 匹配多IP www.2cto.com
 [php]
/**  * 获得访问的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"]);  }  /**
 * 获得访问的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"]);
}


在需要检测的地方 加上调用 check_ip(); 即可;  本函数提供只允许指定的IP访问文件,并提供IP中*号通配符 匹配多IP

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.