PHP solution for obtaining real IP addresses of user clients

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 use a relatively complete method to obtain the customer's ip address. the following section will share with you the PHP method to obtain the real IP address of the user's client. it is not a simple task to obtain the client ip address, because of Ip spoofing and proxy problems, obtaining the authenticity of the client IP address 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 we need to explain this code. here we use two functions: getenv () and strcasecmp (). The previous function gets the environment variables of the system. if the 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.

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 '];}/* handle multi-layer proxies or use regular expressions: $ ip = pre G_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 address
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 (when multiple proxy servers are used, 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 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 address
HTTP_X_FORWARDED_FOR = 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, 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 address

HTTP_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.

The above is a small part of the PHP solution for getting the real IP address of the user client. I hope it will be helpful to you. if you have any questions, please leave a message for me, the editor will reply to you in a timely manner. I would like to thank you for your support for PHP chinnet!

For more articles about how to use PHP to obtain the real IP address of a user's client, refer to the PHP Chinese website!

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.