How to Use PHP to obtain the real IP address of a user Client

Source: Internet
Author: User

Obtaining the Client IP address is not a simple task. Because of IP Spoofing and proxy problems, obtaining the Client IP address authenticity will be compromised and cannot be accurate. however, we try to find a perfect method to obtain the real IP address of the client. you can find many methods to obtain IP addresses using PHP.

 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 );

Now you needCodeHere, two functions are used: getenv () and strcasecmp (). The previous function obtains the environment variables of the system. If a value can be obtained, this value is returned, if not, false is returned.

$ _ ServerIs an array of super global variables on the server.$ _ Server ['remote _ ADDR ']The Client IP Address can also be obtained. The difference between the two is that,GetenvPHP.

Strcasecmp (string1, string2)The usage of string functions isString1AndString2For comparison, if equal, return 0, ifString1GreaterString2, Returns a number greater than 0. If it is smaller than 0, returns a number smaller than 0.

The function uses the Client IP address first. If the client IP address is not valid, try the proxy method. If not, useRemote_addr.

I also saw a more detailed method for detecting IP addresses, considering IP spoofing, which is similar to multiple proxy codes.

 Function  Getip (){  $ Unknown = 'Unknown' ;  If ( Isset ( $ _ Server ['HTTP _ x_forwarded_for ']) & $ _ Server ['HTTP _ x_forwarded_for '] & Strcasecmp ( $ _ Server ['HTTP _ x_forwarded_for '],$ Unknown  )){  $ IP = $ _ Server ['HTTP _ x_forwarded_for' ];}  Elseif ( Isset ( $ _ Server ['Remote _ ADDR ']) & $ _ Server ['Remote _ ADDR '] & Strcasecmp ( $ _ Server ['Remote _ ADDR '], $ Unknown  )){ $ IP = $ _ Server ['Remote _ ADDR' ];}  /*  When processing multi-layer proxies or using regular expressions: $ IP = preg_match ("/[\ D \.] {7, 15}/", $ IP, $ matches )? $ Matches [0]: $ unknown;  */  If ( False ! = Strpos ( $ IP ,',' ))  $ IP = Reset (Explode (',', $ IP  ));  Return   $ IP  ;} 

1. php that does not use the proxy server to obtain the Client IP Address: 

Remote_addr= Client IP
Http_x_forwarded_for= No value or no display

Ii. Transparent proxy server: transparent proxies

Remote_addr= IP address of the last Proxy Server
Http_x_forwarded_for= Real client IP address (this value is similar to 221.5.252.160, 203.98.1820.3, 203.129.72.215 when multiple proxy servers are used)
This type of proxy server still sends the client's real IP address to the access object, which cannot hide the real identity.

3. Use PHP on the normal anonymous proxy server to obtain the Client IP Address: anonymous proxies

Remote_addr= IP address of the last Proxy Server
Http_x_forwarded_foR = Proxy Server IP address (when multiple proxy servers are used, this value is similar to: 203.98.1820.3, 203.98.1820.3, 203.129.72.215)
In this case, the real IP address of the client is hidden, but the client uses a proxy server to access the client.

Iv. destorting proxies

Remote_addr= Proxy Server IP
Http_x_forwarded_for= Random IP address (this value is similar to 220.4.251.159, 203.98.183163, 203.129.72.215 when multiple proxy servers are used)
In this case, we also revealed that the client uses a proxy server, but fabricated a false random IP address (220.4.251.159) instead of the real IP address of the client to cheat it.

5. Use PHP on the highly anonymous proxy server to obtain the Client IP Address: High anonymity proxies (elite proxies)

Remote_addr= Proxy Server IP
Http_x_forwarded_for= No value or no display

Whether it isRemote_addrOrHttp_forwarded_forThese header messages may not be obtained because different network devices in different browsers may send different IP header messages. therefore, PHP uses $ _ server ["remote_addr"] and $ _ server ["http_x_forwarded_for"] to obtain a null value or an "unknown" value.

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.