php code to get user IPv4 or IPv6 address

高洛峰
Release: 2023-03-02 17:36:01
Original
3838 people have browsed it

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

Related labels:
php
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!