Home  >  Article  >  Backend Development  >  Share how to verify email address in php

Share how to verify email address in php

WBOY
WBOYOriginal
2016-07-25 08:56:51875browse
This article shares some php code for verifying email addresses. Friends in need can refer to it.

For example, in the user registration process, we often need to check whether the entered email address is correct. You can use the following code:

<?php
if (eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email)) 
{
   return FALSE;
}
?>

Or this:

<?php 
function checkmail($email){
    return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email);
}
?>

In the above code, it simply checks whether the email format is correct, and does not check the specific domain name. If you want to check the part after @ in something like mailname@domainname.xxx.

You can more accurately determine whether the email address is valid by detecting the domain name. The code is as follows:

<?php
/**
* 通过域名检测邮箱地址的有效性
* edit by bbs.it-home.org
* 用到php函数getmxrr与fsockopen
*/
list($mailusername, $Domainname) = split("@",$myemail);
if(getmxrr($Domainname, $MXHost)) 
{
   return TRUE;
}
else 
{
   if(fsockopen($Domainname, 25, $errno, $errstr, 30)) 
   {
      return TRUE; 
   }
   else 
   {
      return FALSE; 
   }
}
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn