- 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 );
- ?>
-
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:
- 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 '];
- }
- /*
- Handling multi-layer proxies
- Or use the regular expression: $ ip = preg_match ("/[\ d \.] {7, 15}/", $ ip, $ matches )? $ Matches [0]: $ unknown;
- */
- If (false! = Strpos ($ ip ,','))
- $ Ip = reset (explode (',', $ ip ));
- Return $ ip;
- }
- ?>
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. |