Php is a good way to get the user IP address

Source: Internet
Author: User
This article describes how to obtain the IP address of a user in php. For more information, see REMOTE_ADDR. only the IP address set in the local connection of the visitor can be obtained, for example, 10 configured on the campus network of a university. x. XXX. XXX Series IP address. this function obtains the IP address of the Lan gateway egress. if a visitor uses a proxy server, the visitor does not obtain the IP address of the proxy server, but the real IP address of the visitor gateway. If you apply this function to a webpage with limited IP access, other users will not be able to access the webpage even if they access the proxy server in the restricted IP segment.

The following provides a function:

The code is as follows:



// Define a function getIP ()
Function getIP ()
{
Global $ ip;

If (getenv ("HTTP_CLIENT_IP "))
$ Ip = getenv ("HTTP_CLIENT_IP ");
Else if (getenv ("HTTP_X_FORWARDED_FOR "))
$ Ip = getenv ("HTTP_X_FORWARDED_FOR ");
Else if (getenv ("REMOTE_ADDR "))
$ Ip = getenv ("REMOTE_ADDR ");
Else
$ Ip = "Unknow ";

Return $ ip;
}

// Usage:
Echo getIP ();

?>


Getenv ("REMOTE_ADDR") is used to obtain the IP address of the client. However, if the client is accessed by the proxy server, the obtained IP address is the IP address of the proxy server rather than the real IP address of the client. To obtain the real IP address of the client through the proxy server, use getenv ("HTTP_X_FORWARDED_FOR") to read.

However, if the client is not accessed through the proxy server, the value obtained using getenv ("HTTP_X_FORWARDED_FOR") will be null.

The code is as follows:


Else if (getenv ("HTTP_X_FORWARDED_FOR "))
$ Ip = getenv ("HTTP_X_FORWARDED_FOR ");


Indicates that if the value obtained by getenv ("HTTP_X_FORWARDED_FOR") is not null (that is, when the client uses the proxy server), the variable $ ip is equal to getenv ("HTTP_X_FORWARDED_FOR ") the actual IP address.

If the value obtained by the preceding else if (getenv ("HTTP_X_FORWARDED_FOR") is null (that is, no proxy server is used ), the following $ ip = getenv ("HTTP_X_FORWARDED_FOR") is not executed.

In this case, it has been confirmed that the client does not use the proxy server

The code is as follows:


Else if (getenv ("REMOTE_ADDR "))
$ Ip = getenv ("REMOTE_ADDR ");


These two lines of statements obtain the client's IP address, which is also the real IP address.

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.