search
Homephp教程php手册对IP及IP段进行访问限制

本来是写了几个函数,IP黑名单是记到库里的,为了方便理解,改成了一个class把IP黑名单写了出来。

192.168.1.1 单个IP
192.168.1.*      这样代理   192.168.1.1-192.168.1-255
192.158.1.2-20 这样是代表192.158.1.2-192.158.1.20
也可以这样写  192.168.1.[1|2|3]

嘿嘿~一个方法不知道想法是否周全,拿出来大家讨论

使用

以下是引用片段:
$oBlock_ip = new block_ip();
$oBlock_ip->checkIP();

以下是引用片段:
class block_ip {
       var $Block_ip = array("192.168.1.1","210.10.2.1-20","222.34.4.*");
       
       function __construct(){
              
       }
       
       function __destruct(){
              
       }
       
       private function makePregIP($str){
              if (strstr($str,"-")) {
                     $aIP = explode(".",$str);
                     foreach ($aIP as $k=>$v) {
                            if (!strstr($v,"-")) {
                                   $preg_limit .= makePregIP($v);
                            }       else{
                                   $aipNum = explode("-",$v);
                                   for($i=$aipNum[0];$i                                          $preg .=$preg?"|".$i:"[".$i;
                                   }
                                   $preg_limit .=strrpos($preg_limit,".",1)==(strlen($preg_limit)-1)?$preg."]":".".$preg."]";
                            }
                     }
              }else{
                     $preg_limit .= $str.".";
              }
              return $preg_limit;
       }

       private function getAllBlockIP(){
              if ($this->Block_ip) {
                     foreach ($this->Block_ip as $k=>$v) {
                            $ipaddres = $this->makePregIP($v->start_ip);
                            $ip = str_ireplace(".","\.",$ipaddres);
                            $ip  = str_replace("*","[0-9]{1,3}",$ip);
                            $ipaddres  = "/".$ip."/";
                            $ip_list[] = $ipaddres;
                     }
              }
              return $ip_list;
       }

       public function checkIP() {
              $iptable = $this->getAllBlockIP();
              $IsJoined = true;
              //取得用户ip
              $Ip = $this->get_client_ip();
              $Ip = trim($Ip);
              //剔除黑名单中的IP区段
              if ($iptable) {
                     foreach($iptable as $value) {
                            if (preg_match("{$value}",$Ip)) {
                                   $IsJoined = false;
                                   break;
                            }
                     }
              }
              //如果在ip黑名单中就执行如下操作
              if( !$IsJoined ){
                     echo "IP Error";
                     exit;
              }
       }

       private function get_client_ip(){
              if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
              $ip = getenv("HTTP_CLIENT_IP");
              else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
              $ip = getenv("HTTP_X_FORWARDED_FOR");
              else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
              $ip = getenv("REMOTE_ADDR");
              else if (isset($_SERVER[’REMOTE_ADDR’]) && $_SERVER[’REMOTE_ADDR’] && strcasecmp($_SERVER[’REMOTE_ADDR’], "unknown"))
              $ip = $_SERVER[’REMOTE_ADDR’];
              else
              $ip = "unknown";
              return($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 Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

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.