我如何使用PHP获得客户端IP



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

这是我在办公室的资料:

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

我试了所有这些行,但我不能得到真正的ip,只有SERVER网关的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']

以下变量为"undefined":

-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地址。目前我不知道一个可能的解决方案,这个问题没有移动服务器到前面。

如果您正在从服务器运行的机器发出请求,那么$_SERVER['REMOTE_ADDR']显然将包含您自己的IP地址,即192.168.0.1

类似问题

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'];
    }
}

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

相关内容

  • 没有找到相关文章

最新更新