Posted By

paramah on 01/05/09


Tagged

php ip getip


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

jeremyhcobb
umang_nine


new getip php


 / Published in: PHP
 

  1. function getIP()
  2. {
  3. $ip[0] = $_SERVER['REMOTE_ADDR'];
  4. $ip2 = '';
  5. if($_SERVER['HTTP_X_FORWARDED_FOR'])
  6. {
  7. $ip2 = $_SERVER['HTTP_X_FORWARDED_FOR'];
  8. }
  9. if($_SERVER['HTTP_X_ORIGINAL_URI'])
  10. {
  11. if(strlen($ip2) > 0) $ip2 .= ',';
  12. $ip2 .= $_SERVER['HTTP_X_ORIGINAL_URI'];
  13. }
  14. if($_SERVER['HTTP_CLIENT_IP'])
  15. {
  16. if(strlen($ip2) > 0) $ip2 .= ',';
  17. $ip2 .= $_SERVER['HTTP_CLIENT_IP'];
  18. }
  19. $makesafe = false;
  20. $ip3 = explode(',', $ip2);
  21. for($i = 0, $j = sizeof($ip3); $i < $j; $i++)
  22. {
  23. if(!preg_match('/[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}/', $ip3[$i]))
  24. {
  25. $makesafe = true;
  26. }
  27. }
  28. if($makesafe)
  29. {
  30. $ip[1] .= htmlspecialchars(strip_tags(stripslashes($ip2)), ENT_QUOTES);
  31. $ip[2] = false;
  32. }
  33. else
  34. {
  35. $ip[1] = $ip2;
  36. $ip[2] = true;
  37. }
  38. return $ip;
  39. }

Report this snippet  

You need to login to post a comment.