Home > Backend Development > PHP Tutorial > In-depth analysis of the method of obtaining client IP in PHP

In-depth analysis of the method of obtaining client IP in PHP

WBOY
Release: 2016-07-25 08:57:15
Original
1110 people have browsed it
  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']; } elseif
  5. ( isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], $unknown) ) {
  6. $ip = $_SERVER['REMOTE_ADDR'];
  7. }
  8. /* Handle multi-layer proxy situations or use regular methods: $ip = preg_match("/[/d/.]{7,15 }/", $ip, $matches) ? $matches[0] : $unknown; */
  9. if (false !== strpos($ip, ','))
  10. $ip = reset(explode(',' , $ip)); return $ip;
  11. }
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.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template