PHP获取用户访问IP,获取客户端IP

  1. <?php
  2. if ($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"])
  3. {
  4. $ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
  5. }
  6. elseif ($HTTP_SERVER_VARS["HTTP_CLIENT_IP"])
  7. {
  8. $ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];
  9. }
  10. elseif ($HTTP_SERVER_VARS["REMOTE_ADDR"])
  11. {
  12. $ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];
  13. }
  14. elseif (getenv("HTTP_X_FORWARDED_FOR"))
  15. {
  16. $ip = getenv("HTTP_X_FORWARDED_FOR");
  17. }
  18. elseif (getenv("HTTP_CLIENT_IP"))
  19. {
  20. $ip = getenv("HTTP_CLIENT_IP");
  21. }
  22. elseif (getenv("REMOTE_ADDR"))
  23. {
  24. $ip = getenv("REMOTE_ADDR");
  25. }
  26. else
  27. {
  28. $ip = "Unknown";
  29. }
  30. echo "你的IP:".$ip ;
  31. ?>
  32. function getip() {
  33. $unknown = 'unknown';
  34. if ( isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], $unknown) ) {
  35. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  36. } elseif ( isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], $unknown) ) {
  37. $ip = $_SERVER['REMOTE_ADDR'];
  38. }
  39. /*
  40. 处理多层代理的情况
  41. 或者使用正则方式:$ip = preg_match("/[\d\.]{7,15}/", $ip, $matches) ? $matches[0] : $unknown;
  42. */
  43. if (false !== strpos($ip, ','))
  44. $ip = reset(explode(',', $ip));
  45. return $ip;
  46. }
  47. $iipp=$_SERVER["REMOTE_ADDR"];
  48. echo $iipp;

 

你可能感兴趣的:(PHP,IP,客户端)