Home >Backend Development >PHP Tutorial >php code to get user IPv4 or IPv6 address
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;
}
?>