function validate_ip($ip, $stored_ip, $strength = 3) { $ip_pcre_test = preg_match('/([0-9]*){1,3}\.([0-9]*){1,3}\.([0-9]*){1,3}\.([0-9]*){1,3}/', $ip); $stored_pcre_test = preg_match('/([0-9]*){1,3}\.([0-9]*){1,3}\.([0-9]*){1,3}\.([0-9]*){1,3}/', $stored_ip); if ( !$ip_pcre_test || !$stored_pcre_test ) { return false; } $ip = explode('.', $ip); $stored_ip = explode('.', $stored_ip); // This switch statement allows for validation levels from // one to four octets, usualy you would use about 3 to ensure // proper ip-based security. switch ($strength) { case 4: if ( $ip[3] <> $stored_ip[3] ) { return false; } case 3: if ( $ip[2] <> $stored_ip[2] ) { return false; } case 2: if ( $ip[1] <> $stored_ip[1] ) { return false; } case 1: if ( $ip[0] <> $stored_ip[0] ) { return false; } } return true; }