Home > php教程 > PHP源码 > body text

PHP accurately checks whether the email address exists

大家讲道理
Release: 2016-11-08 14:02:24
Original
1387 people have browsed it

Background

There are many ways to verify email addresses in PHP. The more commonly used one is to write regular expressions yourself. However, regular expressions are troublesome. PHP has its own method for verification.

filter_var

filter_var is a variable filtering method built into PHP. It provides many practical filters and can be used to verify integers, floating point numbers, email addresses, URLs, MAC addresses, etc.

Specific filter reference: filters.validate

If filter_var returns false, it means that the variable cannot pass the filter, which means it is illegal.




$email = "lastchiliarch@163.com";
 
var_dump(filter_var($email,  FILTER_VALIDATE_EMAIL));
 
 
$email = "asb";
var_dump(filter_var($email,  FILTER_VALIDATE_EMAIL));
 
$email = "1@a.com";
var_dump(filter_var($email,  FILTER_VALIDATE_EMAIL));
Copy after login

Output:

string(21) "lastchiliarch@163.com"

bool(false)

string(7) "1@a.com"

For the illegal email format asb, false is returned, but for 1@a.com, it passes, but there are still some flaws.

However, if the general rules are passed, 1@a.com will be considered to be a legitimate email address. So is there any way to verify it more accurately?

checkdnsrr

checkdnsrr is actually used to query the DNS record of the specified host. We can use it to verify whether the mailbox exists.

For 1@a.com, the MX record definitely does not exist.

   
$email = "lastchiliarch@163.com";
    var_dump(checkdnsrr(array_pop(explode("@",$email)),"MX"));
    $email = "1@a.com";
    var_dump(checkdnsrr(array_pop(explode("@",$email)),"MX"));
Copy after login

Output:

bool(true)

bool(false)

You can see that it is perfect. The only drawback is that it is too slow. After all, it is a network request. Therefore, it is not suitable to use this method to verify a large number of mailboxes simultaneously.

filter_var+checkdnsrr

We can combine filter_var and checkdnsrr for verification. The vast majority of illegal mailboxes will definitely fail when filter_var is used, and the rest can be further judged using

checkdnsrr.

$email_arr = array("lastchiliarch@163.com", "1@a.com");
    foreach($email_arr as $email) {
        if (filter_var($email) === false) {
            echo "invalid email: $email \n";
            continue;
        }
 
        if(checkdnsrr(array_pop(explode("@",$email)),"MX") === false) {
            echo "invalid email: $email \n";
            continue;
        }
    }
Copy after login

Output: invalid email: 1@a.com

But it should be noted that since we only check the MX record, we can only judge that 163.com exists, but it cannot explain the lastchiliarch This user exists.

If you want to more accurately determine the existence of the mailbox, you can only connect to the SMTP server to verify it.

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template