We Recommend

The Rails Way The Rails Way
Now, for the first time, there’s a comprehensive, authoritative guide to building production-quality software with Rails. Pioneering Rails developer Obie Fernandez and a team of experts illuminate the entire Rails API, along with the Ruby idioms, design approaches, libraries, and plug-ins that make Rails so valuable.


Ballyhoo


Posted By

haozi on 05/11/08


Tagged

php textmate


Versions (?)


get_client_ip


Published in: Other 


  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. /* 取X-Forwarded-For中第一个非unknown的有效IP字符串 */
  17. foreach ($arr AS $ip)
  18. {
  19. $ip = trim($ip);
  20.  
  21. if ($ip != 'unknown')
  22. {
  23. $realip = $ip;
  24.  
  25. break;
  26. }
  27. }
  28. }
  29. elseif (isset($_SERVER['HTTP_CLIENT_IP']))
  30. {
  31. $realip = $_SERVER['HTTP_CLIENT_IP'];
  32. }
  33. else
  34. {
  35. if (isset($_SERVER['REMOTE_ADDR']))
  36. {
  37. $realip = $_SERVER['REMOTE_ADDR'];
  38. }
  39. else
  40. {
  41. $realip = '0.0.0.0';
  42. }
  43. }
  44. }
  45. else
  46. {
  47. if (getenv('HTTP_X_FORWARDED_FOR'))
  48. {
  49. $realip = getenv('HTTP_X_FORWARDED_FOR');
  50. }
  51. elseif (getenv('HTTP_CLIENT_IP'))
  52. {
  53. $realip = getenv('HTTP_CLIENT_IP');
  54. }
  55. else
  56. {
  57. $realip = getenv('REMOTE_ADDR');
  58. }
  59. }
  60.  
  61. preg_match("/[d.]{7,15}/", $realip, $onlineip);
  62. $realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';
  63.  
  64. return $realip;
  65. }

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.