Home >Backend Development >PHP Tutorial >php code to get user IPv4 or IPv6 address

php code to get user IPv4 or IPv6 address

高洛峰
高洛峰Original
2016-12-01 11:11:323925browse

Actually, this is very simple, but I have always wanted to use the ipv6-test API to make something to obtain the user's IP address... Unfortunately, what JSON obtains is only the IP of the local server. Forget it, I won’t study it anymore, not to mention the widgets provided by others are quite easy to use. I googled it and found this code, which can obtain the IP address based on the user environment.

For example, if you access www.shiwo.de via IPv6, you will get the user’s IPv6 address

p.s The premise is that the website has done A and AAAA analysis

Copy the code The code is as follows:
function getIP() /* Get client IP*/
{
if (@$_SERVER["HTTP_X_FORWARDED_FOR"])
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
else if (@$_SERVER["HTTP_CLIENT_IP"])
$ip = $ _SERVER["HTTP_CLIENT_IP"];
else if (@$_SERVER["REMOTE_ADDR"])
$ip = $_SERVER["REMOTE_ADDR"];
else if (@getenv("HTTP_X_FORWARDED_FOR"))
$ip = getenv( "HTTP_X_FORWARDED_FOR");
else if (@getenv("HTTP_CLIENT_IP"))
$ip = getenv("HTTP_CLIENT_IP");
else if (@getenv("REMOTE_ADDR"))
$ip = getenv("REMOTE_ADDR") ;
else
$ip = "Unknown";
return $ip;
}
?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn