Home >Backend Development >PHP Tutorial >PHP code to get accurate client IP address

PHP code to get accurate client IP address

WBOY
WBOYOriginal
2016-07-25 09:05:45926browse
  1. /*
  2. func: get_client_ip()
  3. 获取客户端IP地址
  4. */
  5. function get_client_ip()
  6. {
  7. $ip=false;
  8. if(!empty($_SERVER["HTTP_CLIENT_IP"]))
  9. {
  10. $ip = $_SERVER["HTTP_CLIENT_IP"];
  11. }
  12. if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
  13. {
  14. $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
  15. if ($ip)
  16. {
  17. array_unshift($ips, $ip); $ip = FALSE;
  18. }
  19. for ($i = 0; $i < count($ips); $i++)
  20. {
  21. if (!eregi ("^(10|172.16|192.168).", $ips[$i]))
  22. {
  23. $ip = $ips[$i];
  24. break;
  25. }
  26. }
  27. }
  28. return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
  29. }
  30. ?>
复制代码


Statement:
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