PHP obtains the real IP address of the client.

Source: Internet
Author: User

Remote_addr can only obtain the IP address set in the visitor's local connection, for example, 10 configured on the campus network of Central South University for Nationalities. x. xxx. XXX series IP address. This function obtains the IP address at the LAN gateway egress,
If a visitor uses a proxy server, the visitor obtains the real IP address of the visitor gateway instead of the IP address of the proxy server. 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.

 
// Define a function getip () {Global $ IP; If (getenv ("http_client_ip") $ IP = getenv ("http_client_ip "); else if (getenv ("register") $ 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.

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, you have confirmed that the client does not use the proxy server, and then use
else if (getenv ("remote_addr "))
$ IP = getenv ("remote_addr");
the IP addresses of the clients obtained by these two statements are also real IP addresses.

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.