- 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 proxy situations or use regular methods: $ip = preg_match("/[/d/.]{7,15 }/", $ip, $matches) ? $matches[0] : $unknown; */
- if (false !== strpos($ip, ','))
- $ip = reset(explode(',' , $ip)); return $ip;
- }
Copy code
Note: You can also use the function getenv(’HTTP_X_FORWARDED_FOR’) or getenv(’REMOTE_ADDR’) to achieve the same effect as the above code. However, it is important to note that getenv() does not support PHP running in IIS isapi mode. |