Php code for getting an IPv4 or IPv6 address _ PHP Tutorial

Source: Internet
Author: User
Php obtains the user's IPv4 or IPv6 address code. In fact, this is very simple, but I have always wanted to use the ipv6-test API to get a user IP address ...... But what JSON gets is the IP address of the local server. Forget it, don't study, in fact this is very simple, but I always want to use the ipv6-test API to do a get user IP address ...... But what JSON gets is the IP address of the local server. Forget it, don't study it, and the widgets provided by others are also quite useful. Google finds this code and obtains the IP address based on the user's environment.

For example, when IPv6 accesses www.shiwo.de, the user's IPv6 address is obtained.

P.s. The premise is that the website performs A and AAAA resolution.

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

The ipv6-test's API is something to get the user's IP address ...... But what JSON gets is the IP address of the local server. Forget it, don't study it ,...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.