Posted By

haozi on 05/11/08


Tagged

ip user


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

lfatr


get_client_ip


 / Published in: PHP
 

获取用户真实的IP

  1. function get_client_ip()
  2. {
  3. static $realip = NULL;
  4.  
  5. if ($realip !== NULL)
  6. {
  7. return $realip;
  8. }
  9.  
  10. if (isset($_SERVER))
  11. {
  12. if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
  13. {
  14. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  15.  
  16. foreach ($arr AS $ip)
  17. {
  18. $ip = trim($ip);
  19.  
  20. if ($ip != 'unknown')
  21. {
  22. $realip = $ip;
  23.  
  24. break;
  25. }
  26. }
  27. }
  28. elseif (isset($_SERVER['HTTP_CLIENT_IP']))
  29. {
  30. $realip = $_SERVER['HTTP_CLIENT_IP'];
  31. }
  32. else
  33. {
  34. if (isset($_SERVER['REMOTE_ADDR']))
  35. {
  36. $realip = $_SERVER['REMOTE_ADDR'];
  37. }
  38. else
  39. {
  40. $realip = '0.0.0.0';
  41. }
  42. }
  43. }
  44. else
  45. {
  46. if (getenv('HTTP_X_FORWARDED_FOR'))
  47. {
  48. $realip = getenv('HTTP_X_FORWARDED_FOR');
  49. }
  50. elseif (getenv('HTTP_CLIENT_IP'))
  51. {
  52. $realip = getenv('HTTP_CLIENT_IP');
  53. }
  54. else
  55. {
  56. $realip = getenv('REMOTE_ADDR');
  57. }
  58. }
  59.  
  60. preg_match("/[d.]{7,15}/", $realip, $onlineip);
  61. $realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';
  62.  
  63. return $realip;
  64. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: coldpeer on May 11, 2008

Why it's in Other category rather than PHP? (http://snipplr.com/all/language/php)

You need to login to post a comment.