PHP Solution for obtaining the real IP address of the user client, and php Solution for obtaining the ip address
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 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. Thank you very much for your support for the help House website!