PHP to determine the existence of e-mail address method, PHP email address _php Tutorial

Source: Internet
Author: User
Tags php email mx record

PHP to determine the existence of the e-mail address method, PHP e-mail address


PHP Check the e-mail address of a lot of methods, more commonly used is to write their own regular, but it is more troublesome, I have a method of PHP to do calibration.

Filter_var

Filter_var is a PHP built-in variable filtering method, provides a lot of practical filters, can be used to verify the integer, floating point, Mailbox, URL, MAC address and so on.

Specific filter Reference: Filters.validate

Filter_var If you return false, it means that the variable cannot pass through the filter, which 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)); Output:  string (+) "lastchiliarch@163.com" bool (FALSE) string (7) "1@a.com"

For ASB This illegal mailbox format returned false, but for the 1@a.com passed, or slightly flawed ah.

But the general regular also through will think 1@a.com is a legal mailbox, then what method can be more accurate verification?

Checkdnsrr

CHECKDNSRR is actually used to query the DNS record of the specified host, and we can borrow it to verify that the mailbox exists.

For 1@a.com, the MX record must 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");   Output:  bool (TRUE)  bool (FALSE)

Can see, very perfect, the only drawback is too slow, after all, is to do a network request. So it is not appropriate to synchronize to a large number of mailboxes to use this practice to verify.

Filter_var+checkdnsrr

We can engage Filter_var and CHECKDNSRR do calibration, for the vast majority of illegal mailbox will certainly be in the Filter_var when the time to hang off, the rest of the use

CHECKDNSRR further judgment.

$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 ("@", $email), "MX") = = = False) {      echo "invalid email: $email \ n";      Continue;    }  }   Output: Invalid email:1@a.com

However, it is important to note that because only the MX record is checked, it is only possible to judge that the 163.com is present, but it does not indicate that the Lastchiliarch user is present.

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

Below for everyone to share the PHP e-mail address regular expression validation, the specific content is as follows

<?php header ("content-type:text/html; Charset=utf-8 "); $reply = ""; if (Isset ($_post["email_address")) {  $email _address = $_post["email_address"];  $pattern = "/^ ([0-9a-za-z\\-_\\.] +) @ ([0-9a-z]+\\. [A-z] {2,3} (\\. [A-z] {2})?) $/i ";  if (Preg_match ($pattern, $email _address))  {   $reply = "The email address you entered is legal

\ n "; $user _name = preg_replace ($pattern, "$", $email _address); $domain _name = preg_replace ($pattern, "$", $email _address); $reply. = "User name:" $user _name. "
\ n "; $reply. = "Domain name:" $domain _name. "
\ n "; } else { $reply = "The e-mail address you entered is not valid"; }}? >e-mail Address verification Program

e-mail Address verification Program

<?php echo $reply;? >

I hope you like this article, for everyone to help.

Articles you may be interested in:

    • The implementation and explanation of the regular expression of email address in PHP
    • The PHP code that collects the mailbox (crawl the email address in the webpage)
    • Get an email address based on PHP curl
    • How to implement JS and PHP email address verification
    • PHP combined regular Batch crawl email address in web page
    • Regular expression validation for PHP e-mail addresses

http://www.bkjia.com/PHPjc/1099071.html www.bkjia.com true http://www.bkjia.com/PHPjc/1099071.html techarticle PHP to determine the existence of the e-mail address method, PHP email address php Check the method of many, more commonly used is to write their own regular, but is more troublesome, I have PHP self-brought ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.