Php obtains the real ip address of the client.

Source: Internet
Author: User
Some ideas about how to obtain the real ip address of the client are not necessarily correct, but at least the correct rate is much better. The code is as follows:


Function GetIP (){
If (getenv ("HTTP_CLIENT_IP") & strcasecmp (getenv ("HTTP_CLIENT_IP"), "unknown "))
$ Ip = getenv ("HTTP_CLIENT_IP ");
Else if (getenv ("HTTP_X_FORWARDED_FOR") & strcasecmp (getenv ("HTTP_X_FORWARDED_FOR"), "unknown "))
$ Ip = getenv ("HTTP_X_FORWARDED_FOR ");
Else if (getenv ("REMOTE_ADDR") & strcasecmp (getenv ("REMOTE_ADDR"), "unknown "))
$ Ip = getenv ("REMOTE_ADDR ");
Else if (isset ($ _ SERVER ['remote _ ADDR ']) & $ _ SERVER ['remote _ ADDR '] & strcasecmp ($ _ SERVER ['remote _ ADDR'], "unknown "))
$ Ip = $ _ SERVER ['remote _ ADDR '];
Else
$ Ip = "unknown ";
Return ($ ip );
}


Regist = off
If ($ register_globals! = 1 ){
@ Extract ($ _ SERVER, EXTR_SKIP );
@ Extract ($ _ COOKIE, EXTR_SKIP );
@ Extract ($ _ SESSION, EXTR_SKIP );
@ Extract ($ _ POST, EXTR_SKIP );
@ Extract ($ _ FILES, EXTR_SKIP );
@ Extract ($ _ GET, EXTR_SKIP );
@ Extract ($ _ ENV, EXTR_SKIP );
}
Among them, REMOTE_ADDR is easy to understand. The php manual shows that it is a predetermined variable, while HTTP_x_FORWARDED_FOR finds some information on the Internet.
Use $ _ SERVER ["REMOTE_ADDR"] in PHP to obtain the IP address of the client. However, if the client is accessed by a proxy SERVER, the IP address of the proxy SERVER is obtained, instead of the real client IP address. To obtain the real IP address of the client through the proxy SERVER, use $ _ SERVER ["HTTP_X_FORWARDED_FOR"] to read it.
However, not every proxy SERVER can use $ _ SERVER ["HTTP_X_FORWARDED_FOR"] to read the real IP address of the client, some of the IP addresses read by this method are still the proxy server IP addresses.

As for HTTP_CLIENT_IP, there is a post
'Http _ CLIENT_IP 'is the user's IP address, and 'http _ X_FORWARDED_FOR' is the proxy's IP address.
These IP header messages may not be obtained (because different browsers may send different IP header messages to different network devices ). so PHP tries to judge each IP header message. If yes, take one of them.

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.