联系我们:416326801#qq.com(#改成@)

img1

PHP获取客户端IP地址

1023 Views 标签: PHP


public static function getIp() {
    if( getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown") ) {
        $ip = getenv("HTTP_CLIENT_IP");
    } elseif( getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown") ) {
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    } elseif( getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown") ) {
        $ip = getenv("REMOTE_ADDR");
    } elseif( isset($_SERVER ['REMOTE_ADDR']) && $_SERVER ['REMOTE_ADDR'] && strcasecmp($_SERVER ['REMOTE_ADDR'], "unknown") ) {
        $ip = $_SERVER ['REMOTE_ADDR'];
    } else {
        $ip = "unknown";
    }
    return ($ip);
}

getenv("HTTP_CLIENT_IP")和getenv("HTTP_X_FORWARDED_FOR")可以伪造!

curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:8.8.8.8', 'CLIENT-IP:8.8.8.8'));

如果觉得我的文章对您有用,打赏一下呗.

wxpay.png