search
Homephp教程php手册php限制IP和IP段的代码

php限制IP段代码是我在网上搜相关解决方法时搜到的,这个类的makePregIP函数逻辑有点问题,我修改了下可以使用了,这个类得功能是允许白名单中的IP地址访问,如果要实现限制黑名单中的IP地址访问,简单修改下checkIP函数中的代码逻辑就可以了.

使用方法,代码如下:

<?php
$allow_ip = array(
    "192.168.1.1",
    "210.10.2.1-20",
    "222.34.4.*",
    "127.0.0.1"
);
$oBlock_ip = new allowIp($allow_ip);
if (!$oBlock_ip->checkIP()) {
    echo &#39;您的IP为:&#39;;
    echo $oBlock_ip->ip;
    exit(&#39;禁止访问&#39;);
}
?>

allowIP类文件,代码如下:

<?php
class allowIp {
    function __construct($allow_ip) {
        if (emptyempty($allow_ip)) {
            return false;
        }
        $this->allow_ip = $allow_ip;
        $this->ip = &#39;&#39;;
    }
    private function makePregIP($str) {
        if (strstr($str, "-")) {
            $aIP = explode(".", $str);
            foreach ($aIP as $k => $v) {
                if (!strstr($v, "-")) {
                    $preg_limit.= $this->makePregIP($v);
                    $preg_limit.= ".";
                } else {
                    $aipNum = explode("-", $v);
                    for ($i = $aipNum[0]; $i <= $aipNum[1]; $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->allow_ip) {
            $i = 1;
            foreach ($this->allow_ip as $k => $v) {
                $ipaddres = $this->makePregIP($v);
                $ip = str_ireplace(".", ".", $ipaddres);
                $ip = str_replace("*", "[0-9]{1,3}", $ip);
                $ipaddres = "/" . $ip . "/";
                $ip_list[] = $ipaddres;
                $i++;
            }
        }
        return $ip_list;
    }
    public function checkIP() {
        $iptable = $this->getAllBlockIP();
        $IsJoined = false;
        //取得用户ip
        $Ip = $this->get_client_ip();
        $Ip = trim($Ip);
        //在白名单中
        if ($iptable) {
            foreach ($iptable as $value) {
                if (preg_match("{$value}", $Ip)) {
                    $IsJoined = true;
                    break;
                }
            }
        }
        //不在白名单中
        if (!$IsJoined) {
            return false;
        }
        return true;
    }
    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[&#39;REMOTE_ADDR&#39;]) && $_SERVER[&#39;REMOTE_ADDR&#39;] && strcasecmp($_SERVER[&#39;REMOTE_ADDR&#39;], "unknown")) $ip = $_SERVER[&#39;REMOTE_ADDR&#39;];
        else $ip = "unknown";
        $this->ip = $ip;
        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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools