获取用户IP(相对真实)


    /** 
    * 获取当前用户真实 IP
    *
    * @return string $ip
    */
    public function  get_client_ip ( )
    {
        $ip = $_SERVER['REMOTE_ADDR'];
        if ( isset( $_SERVER['HTTP_CLIENT_IP'] ) && preg_match('/^([0-9]{1,3}.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP']) ) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) && preg_match_all('/^([0-9]{1,3}.){3}[0-9]{1,3}$/', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches) ) {
            foreach ( $matches[0] AS $xip ) {
                if ( !preg_match('#^(10|172.16|192.168).#', $xip) ) {
                    $ip = $xip;
                    break;
                }
            }
        }
    return $ip;
    }


你可能感兴趣的:(获取用户IP(相对真实))