1. <?php
  2. /**
  3.  * is_ipaddr() Function
  4.  * Created by: Joshua H. (TRUSTAbyss)
  5.  *
  6.  * This function returns a boolean value.
  7.  * Usage: is_ipaddr("IP");
  8.  */
  9.  
  10. function is_ipaddr($ip)
  11. {
  12. $result = FALSE;
  13.  
  14. // Check to see if the IP Address is valid.
  15. if (preg_match("/^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/", $ip, $matches))
  16. {
  17. // Check each of the IP digits to see if they're less than or equal to 255 (valid IP Range).
  18. if (($matches[1] <= 255) && ($matches[2] <= 255) && ($matches[3] <= 255) && ($matches[4] <= 255))
  19. {
  20. $result = TRUE; // Valid IP.
  21. }
  22. }
  23.  
  24. return $result;
  25. }
  26.  
  27. // Example code below.
  28.  
  29. if (is_ipaddr("65.5.240.229"))
  30. {
  31. echo "This is a valid IP Address. ";
  32.  
  33. }
  34. else
  35. {
  36. echo "This is NOT a valid IP Address.";
  37. }
  38. ?>