php gets the code for the user's IPv4 or IPv6 address

  • 2020-05-26 08:06:36
  • OfStack

Actually this is very simple, except I want to use ipv6-test's API to make a thing to get the user's IP address... But all JSON got was the IP of the local server. Forget it, I won't study it. Besides, widget provided by others is quite useful. Google 1, find this code, can be based on the user environment to get the IP address.

For example, when IPv6 visits www.shiwo.de, it gets the user's IPv6 address

p.s if the site does A and AAAA parsing

 
<?php 
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; 
} 
?> 

Related articles: