Home > Backend Development > PHP Tutorial > php获取用户IPv4或IPv6地址的代码_PHP

php获取用户IPv4或IPv6地址的代码_PHP

WBOY
Release: 2016-06-01 12:09:16
Original
1310 people have browsed it

IPv6

其实这个很简单,只不过我一直想用 ipv6-test 的API做一个获取用户IP地址的东西……无奈JSON获取到的只是本机服务器的IP。算了,不研究了,何况人家提供的widget也挺好用的。谷歌了一下,找到这段代码,可以根据用户环境获取IP地址。

例如IPv6访问 www.shiwo.de 则获取到的是用户的IPv6地址

p.s 前提是网站做了A和AAAA解析

复制代码 代码如下:
function getIP() /*获取客户端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 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