Home > Backend Development > PHP Tutorial > PHP code to obtain user IPv4 or IPv6 address_PHP tutorial

PHP code to obtain user IPv4 or IPv6 address_PHP tutorial

WBOY
Release: 2016-07-21 15:14:18
Original
1103 people have browsed it

In fact, this is very simple, but I have always wanted to use the ipv6-test API to make something that obtains the user's IP address... Unfortunately, JSON only obtains 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 according to the user environment.

For example, if IPv6 accesses www.shiwo.de, the user's IPv6 address will be obtained

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

Copy code The code is as follows:

function getIP() /*Get the 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;
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326295.htmlTechArticleActually, 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, no more research,...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template