Zend - The PHP Company




Miscellaneous

Add Code


data verification functions  

Type: code fragment
Added by: robF
Entered: 16/11/2000
Last modified: 01/12/2000
Rating: - (fewer than 3 votes)
Views: 6724
a few simple functions to check the validity of internet hostnames, IP addresses etc.


<?php
function is_hostname($hostname) {
 
    
/* Internet hostnames must be made up of "." seperated tokens, each
     * which must begin with a letter, end with a number or letter, and
     * contain only digits, letters and hyphens. This function checks for
     * those conditions and returns false if any of them are not met
     */
 
    
$tokens explode("."$hostname);
 
    for (
$error $j 0$j sizeof($tokens); $j++) {
 
        if ((!
ereg("^[a-zA-Z]"$tokens[$j])) || ereg("[^a-zA-Z0-9-]",
        
$tokens[$j]) || (!ereg("[a-zA-Z0-9]$"$tokens[$j]))):
            
$error 1;
            break;
        endif;
 
    }
 
    return (
$error == 0) ? true false;
 
}
 
function 
is_mac_address($mac_addr "0") {
 
    
/* Returns true if the argument is a valid MAC address, false otherwise.
     * Looks for six colon separated positive hex numbers with values less
     * than or eqaul to  255 (The +ve check should be redundant, but you
     * never know.)
     */
 
    
$eth_numbers explode(":"$mac_addr);
 
    
$ret_code = (sizeof($eth_numbers) == 6) ? true false;
 
    for (
$j 0$j sizeof($eth_numbers); $j++) {
 
        if (
hexdec($eth_numbers[$j]) > 255 || $eth_numbers[$j] < ||
        
ereg("[^0-9a-fA-F]"$eth_numbers[$j]) || $eth_numbers[$j] == ""
        
|| $ret_code == false):
            
$ret_code false;
            break;
        endif;
 
    }
 
    return 
$ret_code;
 
}                   

function 
is_ip_address($ip_addr "0") {
 
    
/* Returns true if the argument is a valid IP address, false otherwise.
     * Looks for four dot separated positive decimal integers less than or
     * equal to  255 (The +ve check should be redundant, but you never
     * know.)
     */
 
    
$ip_numbers explode("."$ip_addr);
 
    
$ret_code = (sizeof($ip_numbers) == 4) ? true false;
 
    for (
$j 0$j sizeof($ip_numbers); $j++) {
 
        if (
$ip_numbers[$j] > 255 || $ip_numbers[$j] < 0
        
|| ereg('[^0-9]'$ip_numbers[$j]) || $ip_numbers[$j] == ""
        
|| $ret_code == false):
            
$ret_code false;
            break;
        endif;
 
    }
 
    return 
$ret_code;
 
}
 
function 
is_device($device "null") {
 
    
/* Returns true if the argument is a valid Solaris disk device. e.g.
     * c0t0d0 */
 
    
return (ereg("^c[0-9]+t[0-9]+d[0-9]+$"$device)) ? true false;
 
}                        

?>


Usage Example


if (is_hostname($host)):
    echo "hostname is valid";
else:
    echo "please enter a valid internet hostname";
endif;


Rate This Script





Search



This Category All Categories