Home > Article > Backend Development > PHP verifies the validity and correctness of the email entered by the user_PHP tutorial
function validate_email($email){
$exp="^[a-z'0-9]+([._-][a-z'0-9]+)*@([a-z0-9]+([._-] [a-z0-9]+))+$";
if(eregi($exp,$email)){ //First use regular expression to verify the validity of the email format
if(checkdnsrr(array_pop(explode("@",$email)),"MX")){//Use checkdnsrr to verify the validity of the domain name part of the email
return true;
}else{
return false;
}
}else{
return false;
}
}
Note: The checkdnsrr function is invalid on the win host! The following is a solution proposed by a foreign programmer. He also wrote a function to replace the checkdnsrr function:
function myCheckDNSRR($hostName, $recType=''){
if(!emptyempty($hostName)){
if( $recType=='' ) $recType="MX";
exec("nslookup -type=$recType $hostName", $result);
foreach($result as $line){
if(eregi("^$hostName",$line)){
return true;
}
}
return false;
}
return false;
}