Home > Article > Backend Development > php能否从外网获取用户的内网ip地址解决思路
php能否从外网获取用户的内网ip地址
假设用户通过路由等设备连接上网,能不能通过php来获得访问用户的内网ip,即198.xxx.xxx.xxx这样的ip。这里是指外网的用户访问,不是获取服务器主机所在的内网!
<br /> function getIP() { <br /> <br /> if (@$_SERVER["HTTP_X_FORWARDED_FOR"]) <br /> $ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; <br /> else if (@$_SERVER["HTTP_CLIENT_IP"]) <br /> $ip = $_SERVER["HTTP_CLIENT_IP"]; <br /> else if (@$_SERVER["REMOTE_ADDR"]) <br /> $ip = $_SERVER["REMOTE_ADDR"]; <br /> else if (@getenv("HTTP_X_FORWARDED_FOR"))<br /> $ip = getenv("HTTP_X_FORWARDED_FOR"); <br /> else if (@getenv("HTTP_CLIENT_IP")) <br /> $ip = getenv("HTTP_CLIENT_IP"); <br /> else if (@getenv("REMOTE_ADDR")) <br /> $ip = getenv("REMOTE_ADDR"); <br /> else <br /> $ip = "Unknown"; <br /> return $ip; <br /> }<br /> <br />