Ip Check Using PHP
If you have ever had the need to validate an IP address then this small script is for you.
1 2 3 4 5 6 7 8 9 | function validate_ip($ip) { if (is_string($ip) && ereg('^([0-9]{1,3}).([0-9]{1,3}).' . '([0-9]{1,3}).([0-9]{1,3})$', $ip, $part)) { if ($part[1] <= 255 && $part[2] <= 225 && $parg[3] <= 255 && $part[4] <= 255) return TRUE; # Valid IP } return FALSE; # Invalid IP } |
Ok, that’s all fine and dandy but what does it mean?
The fist if the statement is breaking the IP up into 3 sections and removing the ‘.’ from the user input. It then stores the results in the $part var then the second If statement checks to see if all of the parts are valid. If everything is fine and dandy it will return TRUE and if something has gone wrong it will return FALSE.
Hope this helps anyone.