Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
699 views
in Technique[技术] by (71.8m points)

php - 使用PHP获取客户端IP地址[重复](Get the client IP address using PHP [duplicate])

This question already has an answer here:

(这个问题在这里已有答案:)

I want to get the client IP address who uses my website.

(我想获得使用我的网站的客户端IP地址。)

I am using the PHP $_SERVER superglobal:

(我正在使用PHP $_SERVER超全局:)

$_SERVER['REMOTE_ADDR'];

But I see it can not give the correct IP address using this.

(但我发现使用它无法提供正确的IP地址。)

I get my IP address and see it is different from my IP address and I can also see my IP address in some website like:

(我得到了我的IP地址,看到它与我的IP地址不同,我也可以在某些网站上看到我的IP地址,例如:)

http://whatismyipaddress.com/

I paste the IP address which give my PHP function but this website shows no result about this.

(我粘贴了我的PHP函数的IP地址,但是这个网站没有显示这个结果。)

How does this problem come about and how can I get IP address of the client?

(这个问题是如何产生的,如何获得客户端的IP地址?)

  ask by user1752627 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The simplest way to get the visitor's/client's IP address is using the $_SERVER['REMOTE_ADDR'] or $_SERVER['REMOTE_HOST'] variables.

(获取访问者/客户端IP地址的最简单方法是使用$_SERVER['REMOTE_ADDR']$_SERVER['REMOTE_HOST']变量。)

However, sometimes this does not return the correct IP address of the visitor, so we can use some other server variables to get the IP address.

(但是,有时这不会返回访问者的正确IP地址,因此我们可以使用其他一些服务器变量来获取IP地址。)

The below both functions are equivalent with the difference only in how and from where the values are retrieved.

(以下两个函数仅相同,只取决于检索值的方式和位置。)

getenv() is used to get the value of an environment variable in PHP.

(getenv()用于获取PHP中环境变量的值。)

// Function to get the client IP address
function get_client_ip() {
    $ipaddress = '';
    if (getenv('HTTP_CLIENT_IP'))
        $ipaddress = getenv('HTTP_CLIENT_IP');
    else if(getenv('HTTP_X_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
    else if(getenv('HTTP_X_FORWARDED'))
        $ipaddress = getenv('HTTP_X_FORWARDED');
    else if(getenv('HTTP_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_FORWARDED_FOR');
    else if(getenv('HTTP_FORWARDED'))
       $ipaddress = getenv('HTTP_FORWARDED');
    else if(getenv('REMOTE_ADDR'))
        $ipaddress = getenv('REMOTE_ADDR');
    else
        $ipaddress = 'UNKNOWN';
    return $ipaddress;
}

$_SERVER is an array that contains server variables created by the web server.

($ _SERVER是一个包含Web服务器创建的服务器变量的数组。)

// Function to get the client IP address
function get_client_ip() {
    $ipaddress = '';
    if (isset($_SERVER['HTTP_CLIENT_IP']))
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    else if(isset($_SERVER['HTTP_X_FORWARDED']))
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    else if(isset($_SERVER['HTTP_FORWARDED']))
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    else if(isset($_SERVER['REMOTE_ADDR']))
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    else
        $ipaddress = 'UNKNOWN';
    return $ipaddress;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...