php 设置白名单ip

php 设置ip白名单

//检查白名单ip
private function _checkAllowIp()
{
    $allowIp = ['203.195.156.12'];
    $ip = $this->getIp();
    if (!in_array($ip, $allowIp)) {
        exit();
    }
}
 
/**
 * 获取客户端访问ip
 */
private function getIp()
{
    if (getenv('HTTP_CLIENT_IP')) {
        $ip = getenv('HTTP_CLIENT_IP');
    } else if (getenv('HTTP_X_FORWARDED_FOR')) {
        $ip = getenv('HTTP_X_FORWARDED_FOR');
    } else if (getenv('REMOTE_ADDR')) {
        $ip = getenv('REMOTE_ADDR');
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    $ips = explode(',', $ip);
    if (count($ips) > 1) {
        $ip = $ips[0];
    }
    return $ip;
}

posted @ 2018-12-09 14:37  绝技小嗨皮  阅读(1585)  评论(0编辑  收藏  举报
Title