How to obtain the real IP address of the client using php

Source: Internet
Author: User
How to obtain the real IP address of the client using php

  1. Function getIp (){
  2. If (getenv ("HTTP_CLIENT_IP") & strcasecmp (getenv ("HTTP_CLIENT_IP"), "unknown "))
  3. $ Ip = getenv ("HTTP_CLIENT_IP ");
  4. Else if (getenv ("HTTP_X_FORWARDED_FOR") & strcasecmp (getenv ("HTTP_X_FORWARDED_FOR"), "unknown "))
  5. $ Ip = getenv ("HTTP_X_FORWARDED_FOR ");
  6. Else if (getenv ("REMOTE_ADDR") & strcasecmp (getenv ("REMOTE_ADDR"), "unknown "))
  7. $ Ip = getenv ("REMOTE_ADDR ");
  8. Else if (isset ($ _ SERVER ['remote _ ADDR ']) & $ _ SERVER ['remote _ ADDR '] & strcasecmp ($ _ SERVER ['remote _ ADDR'], "unknown "))
  9. $ Ip = $ _ SERVER ['remote _ ADDR '];
  10. Else
  11. $ Ip = "unknown ";
  12. Return ($ ip );
  13. ?>

Note: The preceding code uses two functions: 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. $ _ SERVER is an array of super global variables of the SERVER. you can use $ _ SERVER ['remote _ ADDR '] to obtain the IP address of the client. The difference between the two is that getenv does not support php running in IIS isapi mode. The strcasecmp (string1, string2) string function is used to compare string1 and string2. if it is equal, return 0. if string1 is greater than string2, return a number greater than 0, if the value is less than 0, the return value is less than 0. The function uses the client IP address first. if the client IP address is not valid, try the proxy method. if not, use REMOTE_ADDR.

Here is another more detailed method for detecting IP addresses, considering IP spoofing and multiple proxy codes, the methods are similar:

  1. Function getip (){
  2. $ Unknown = 'unknown ';
  3. If (isset ($ _ SERVER ['http _ X_FORWARDED_FOR ']) & $ _ SERVER ['http _ X_FORWARDED_FOR '] & strcasecmp ($ _ SERVER ['http _ X_FORWARDED_FOR'], $ unknown )){
  4. $ Ip = $ _ SERVER ['http _ X_FORWARDED_FOR '];
  5. } Elseif (isset ($ _ SERVER ['remote _ ADDR ']) & $ _ SERVER ['remote _ ADDR '] & strcasecmp ($ _ SERVER ['remote _ ADDR'], $ unknown )){
  6. $ Ip = $ _ SERVER ['remote _ ADDR '];
  7. }
  8. /*
  9. Handling multi-layer proxies
  10. Or use the regular expression: $ ip = preg_match ("/[\ d \.] {7, 15}/", $ ip, $ matches )? $ Matches [0]: $ unknown;
  11. */
  12. If (false! = Strpos ($ ip ,','))
  13. $ Ip = reset (explode (',', $ ip ));
  14. Return $ ip;
  15. }
  16. ?>

Appendix for reference:1. PHP that does not use the proxy server to obtain the client IP address:REMOTE_ADDR = client IPHTTP_X_FORWARDED_FOR = no value or no Display

II. Transparent Proxy Server: Transparent ProxiesREMOTE_ADDR = last proxy server IPHTTP_X_FORWARDED_FOR = client's real IP address (this value is similar to: 221.5.252.160, 203.98.1820.3, 203.129.72.215) 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 ProxiesREMOTE_ADDR = The Last proxy server IPHTTP_X_FORWARDED_FOR = the proxy server IP address (when multiple proxy servers are used, this value is similar to: 203.98.182.163, 203.98.182.163, 203.129.72.215, however, it is revealed to the access object that the client uses the proxy server to access them.

IV. destorting ProxiesREMOTE_ADDR = proxy server protocol = random IP address (when multiple proxy servers are used, this value is similar to: 220.4.251.159, 203.98.1820.3, 203.129.72.215). In this case, the client uses the proxy server, however, a fake random IP address (220.4.251.159) is fabricated to replace the real IP address of the client.

5. use PHP on the highly anonymous proxy server to obtain the client IP address: High Anonymity Proxies (Elite proxies)REMOTE_ADDR = proxy server IPHTTP_X_FORWARDED_FOR = no value or no Display

Whether REMOTE_ADDR or HTTP_FORWARDED_FOR, these 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.