PHP's practical functions for implementing black and white lists

墨辰丷
Release: 2023-03-31 20:14:01
Original
1779 people have browsed it

This article shares with you two practical functions in PHP for implementing black and white lists, namely the security IP detection function and the client IP acquisition function. The explanation is very clear in the comments, so I won’t go into too much nonsense here.

This is a PHP function that detects whether an IP is illegal. It is suitable for the development of whitelist and blacklist functions. The main scenarios are applied to: API source restrictions, access restrictions, etc.

The code is as follows:

/**
 * 安全IP检测,支持IP段检测
 * @param string $ip 要检测的IP
 * @param string|array $ips  白名单IP或者黑名单IP
 * @return boolean true 在白名单或者黑名单中,否则不在
 */
function is_safe_ip($ip="",$ips=""){ 
    if(!$ip) $ip = get_client_ip();  //获取客户端IP
    if($ips){
        if(is_string($ips)){ //ip用"," 例如白名单IP:192.168.1.13,123.23.23.44,193.134.*.*
            $ips = explode(",", $ips);
        }
    }else{ //读取后台配置 白名单IP
        $obj = new Setting();
        $ips = explode(",", $obj->getConfig("whiteip"));  
    }
    if(in_array($ip, $ips)){
        return true;
    }
    $ipregexp = implode('|', str_replace( array('*','.'), array('\d+','\.') ,$ips));  
    $rs = preg_match("/^(".$ipregexp.")$/", $ip);  
    if($rs) return true;
    return ;
}
Copy after login

Get the ip address, here we quote the thinkphp built-in function

The code is as follows:

//应网友要求,贴出 get_client_ip() 函数
/**
 * 获取客户端IP地址
 * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
 * @param boolean $adv 是否进行高级模式获取(有可能被伪装) 
 * @return mixed
 */
function get_client_ip($type = 0,$adv=false) {
    $type       =  $type ? 1 : 0;
    static $ip  =   NULL;
    if ($ip !== NULL) return $ip[$type];
    if($adv){
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $arr    =   explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            $pos    =   array_search('unknown',$arr);
            if(false !== $pos) unset($arr[$pos]);
            $ip     =   trim($arr[0]);
        }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip     =   $_SERVER['HTTP_CLIENT_IP'];
        }elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ip     =   $_SERVER['REMOTE_ADDR'];
        }
    }elseif (isset($_SERVER['REMOTE_ADDR'])) {
        $ip     =   $_SERVER['REMOTE_ADDR'];
    }
    // IP地址合法验证
    $long = sprintf("%u",ip2long($ip));
    $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
    return $ip[$type];
}
Copy after login

Summary: The above is the entire content of this article, I hope it can It will be helpful to everyone’s study.

Related recommendations:

Basic knowledge of streams in PHP

Introduction and role of PHP output buffering

php method to implement UDP communication based on socket

The above is the detailed content of PHP's practical functions for implementing black and white lists. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!