Home >Backend Development >PHP Tutorial >PHP Get visitor IP address

PHP Get visitor IP address

WBOY
WBOYOriginal
2016-07-23 08:54:531317browse

获取访问者IP地址

[PHP]代码

  1. static public function getip()
  2. {
  3. if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
  4. {
  5. $ip = getenv("HTTP_CLIENT_IP");
  6. }
  7. else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
  8. {
  9. $ip = getenv("HTTP_X_FORWARDED_FOR");
  10. }
  11. else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
  12. {
  13. $ip = getenv("REMOTE_ADDR");
  14. }
  15. else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
  16. {
  17. $ip = $_SERVER['REMOTE_ADDR'];
  18. }
  19. else
  20. {
  21. $ip = "";
  22. }
  23. return($ip);
  24. }
复制代码
PHP


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