Home  >  Article  >  Backend Development  >  PHP comes with a method to verify your email address

PHP comes with a method to verify your email address

*文
*文Original
2017-12-29 18:04:202264browse

This article mainly introduces in detail PHP's own method to verify whether the mailbox exists, and PHP's own method to verify whether the URL and IP are legal. Interested friends can refer to it. I hope to be helpful.

There are many ways to verify email addresses in PHP. The most common one is to write regular expressions yourself. However, regular expressions are troublesome. PHP comes with its own method for verification.

filter_var

filter_var is a variable filtering method built into PHP. It provides many practical filters that can be used to check integers. , floating point number, email, URL, MAC address, etc.

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));

Output:


##

string(21) "lastchiliarch@163.com"
bool(false)
string(7) 1@a.com

Returns false for the illegal email format asb , but for 1@a.com, it passed, but there were 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"));

Output:


##

  bool(true)
  bool(false)

As you can see, it’s perfect, the only drawback is It's just too slow. After all, it's a network request. Therefore, it is not suitable to use this method to verify a large number of mailboxes simultaneously.

filter_var+checkdnsrrWe can combine filter_var and checkdnsrr for verification. For most illegal mailboxes, filter_var will definitely be used. It hangs up, and you can use

checkdnsrr to further judge the rest.


##
$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;
    }
  }

Output:

invalid email: 1@a.com

But it should be noted that since it is only checking the MX record, so It can only be determined that 163.com exists, but it cannot prove that the user lastchiliarch exists.

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

Introduced email verification. PHP's own method how to verify whether the email, URL, and IP are legal. Here is an introduction:

The main thing is to use the

filter_var function

.

Syntax

filter_var(variable, filter, options)
variable Required. Specifies the variables to filter. filter Optional. Specifies the ID of the filter to use. options Specifies an array containing flags/options. Check the possible flags and options for each filter.


PHP Filters

Example #1 A filter_var() example


<?php
var_dump(filter_var(&#39;bob@example.com&#39;, FILTER_VALIDATE_EMAIL));
var_dump(filter_var(&#39;http://example.com&#39;, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED));
?>

The above routine will output:

string(15) "bob@example.com"
bool(false)


Related recommendations:

php Code to check if a number is odd or even

php Code to check if a file or directory exists

Basics and simple examples of PHP regular expressions

The above is the detailed content of PHP comes with a method to verify your email address. For more information, please follow other related articles on the PHP Chinese website!

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