1

我有一个带有 PHP 的网页,我试图获取客户端 IP,但我得到的是服务器网关 IP 地址(192.168.0.1)。

这是我在办公室的:

ISP ---> ISP 路由器 ---> 我的路由器(192.168.0.1) ----> (192.168.0.2)我的服务器。

我尝试了所有这些行,但我无法获得真正的 ip,只有 SERVER GATEWAY ip。

$_SERVER['HTTP_X_FORWARDED_FOR']
$_SERVER['HTTP_CLIENT_IP']
$_SERVER['HTTP_X_REAL_IP']
$_SERVER['GATEWAY_INTERFACE']
$_SERVER['SERVER_ADDR']
$_SERVER['SERVER_NAME']
$_SERVER['SERVER_SOFTWARE']
$_SERVER['SERVER_PROTOCOL']
$_SERVER['REQUEST_METHOD']
$_SERVER['REQUEST_TIME']
$_SERVER['QUERY_STRING']
$_SERVER['DOCUMENT_ROOT']
$_SERVER['HTTP_ACCEPT']
$_SERVER['HTTP_ACCEPT_CHARSET']
$_SERVER['HTTP_ACCEPT_ENCODING']
$_SERVER['HTTP_ACCEPT_LANGUAGE']
$_SERVER['HTTP_CONNECTION']
$_SERVER['HTTP_HOST']
$_SERVER['HTTP_REFERER']
$_SERVER['HTTP_USER_AGENT']
$_SERVER['HTTPS']
$_SERVER['REMOTE_ADDR']
$_SERVER['REMOTE_HOST']
$_SERVER['REMOTE_PORT']
$_SERVER['SCRIPT_FILENAME']
$_SERVER['SERVER_ADMIN']
$_SERVER['SERVER_PORT'] 
$_SERVER['SERVER_SIGNATURE']
$_SERVER['PATH_TRANSLATED']
$_SERVER['SCRIPT_NAME']
$_SERVER['REQUEST_URI']
$_SERVER['PHP_AUTH_DIGEST']
$_SERVER['PHP_AUTH_USER'] 
$_SERVER['PHP_AUTH_PW']
$_SERVER['AUTH_TYPE']

以下变量是“未定义的”:

-HTTP_X_FORWARDED_FOR
-HTTP_CLIENT_IP
-HTTP_X_REAL_IP
-HTTPS
-REMOTE_HOST
-PATH_TRANSLATED
-PHP_AUTH_DIGEST
-PHP_AUTH_USER
-PHP_AUTH_PW
-AUTH_TYPE



编辑:尝试了“var_dump($_SERVER)”并且找不到客户端IP地址:(

编辑2:我在谷歌上搜索了一下,我“找到了答案”。用户Martin Hohenberg说的是对的。在NAT过程中标头被破坏,因此我的服务器无法读取原始 IP 地址。目前,如果不将服务器移到前面,我不知道解决此问题的可行方法。

4

2 回答 2

3

类似的问题

尝试

echo $_SERVER['REMOTE_ADDR'];

阅读更多

function getUserIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) //if from shared
    {
        return $_SERVER['HTTP_CLIENT_IP'];
    }
    else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //if from a proxy
    {
        return $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
        return $_SERVER['REMOTE_ADDR'];
    }
}

请检查阅读更多链接和问题链接

于 2013-05-15T11:44:45.150 回答
2

If you are making a request from the machine where the server is running, then $_SERVER['REMOTE_ADDR'] will obviously contain your own IP address, which is 192.168.0.1

于 2013-05-15T11:48:16.963 回答