Home > Article > Backend Development > Share how to verify email address in php
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; } } ?> |