This article is mainly for everyone to introduce the PHP self-band method to verify the existence of the mailbox, as well as PHP's own method to verify the URL, IP is legitimate, interested in small partners can refer to. We hope to help you.
PHP Check email address A lot of methods, more commonly used is to write their own regular, but it is more trouble, PHP has a method to do the check.
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.
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.
Introduces the mailbox authentication, PHP comes with the method how to verify the mailbox, URL, IP is legitimate, the following for you to introduce:
The main use is the Filter_var function .
Grammar
Filter_var (variable, filter, options)
Variable required. Specifies the variables to filter.
Filter is optional. Specifies the ID of the filter to be used.
The options rule contains an array of flags/options. Check the possible flags and options for each filter.
PHP Filters
Example #1 A Filter_var () Example
<?phpvar_dump (Filter_var (' bob@example.com ', filter_validate_email)); Var_dump (Filter_var (' http://example.com ') , Filter_validate_url, filter_flag_path_required));? >
The above routines will output:
String (() "bob@example.com" bool (false)
Related recommendations:
PHP check numbers are odd or even-numbered code
PHP checks whether a file or directory has a code summary
Basic and simple examples of PHP regular expressions