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

PHP comes with a method to verify your email address

Dec 29, 2017 pm 06:04 PM
php address Mail

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));
Copy after login

Output:


##

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

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"));
Copy after login

Output:


##

  bool(true)
  bool(false)
Copy after login

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;
    }
  }
Copy after login

Output:

invalid email: 1@a.com
Copy after login

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));
?>
Copy after login

The above routine will output:

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


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Top 10 Digital Virtual Currency Apps Rankings: Top 10 Digital Currency Exchanges in Currency Circle Trading Top 10 Digital Virtual Currency Apps Rankings: Top 10 Digital Currency Exchanges in Currency Circle Trading Apr 22, 2025 pm 03:00 PM

The top ten digital virtual currency apps are: 1. OKX, 2. Binance, 3. gate.io, 4. Coinbase, 5. Kraken, 6. Huobi, 7. KuCoin, 8. Bitfinex, 9. Bitstamp, 10. Poloniex. These exchanges are selected based on factors such as transaction volume, user experience and security, and all provide a variety of digital currency trading services and an efficient trading experience.

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

bitget new user registration guide 2025 bitget new user registration guide 2025 Apr 21, 2025 pm 10:09 PM

The steps to register for Bitget in 2025 include: 1. Prepare a valid email or mobile phone number and a stable network; 2. Visit the Bitget official website; 3. Enter the registration page; 4. Select the registration method; 5. Fill in the registration information; 6. Agree to the user agreement; 7. Complete verification; 8. Obtain and fill in the verification code; 9. Complete registration. After registering, it is recommended to log in to the account, perform KYC identity verification, and set up security measures to ensure the security of the account.

Top 10 digital currency exchanges Top 10 digital currency app exchanges Top 10 digital currency exchanges Top 10 digital currency app exchanges Apr 22, 2025 pm 03:15 PM

The top ten digital currency exchanges are: 1. OKX, 2. Binance, 3. gate.io, 4. Coinbase, 5. Kraken, 6. Huobi, 7. KuCoin, 8. Bitfinex, 9. Bitstamp, 10. Poloniex. These exchanges are selected based on factors such as transaction volume, user experience and security, and all provide a variety of digital currency trading services and an efficient trading experience.

Can two exchanges convert coins to each other? Can two exchanges convert coins to each other? Can two exchanges convert coins to each other? Can two exchanges convert coins to each other? Apr 22, 2025 am 08:57 AM

Can. The two exchanges can transfer coins to each other as long as they support the same currency and network. The steps include: 1. Obtain the collection address, 2. Initiate a withdrawal request, 3. Wait for confirmation. Notes: 1. Select the correct transfer network, 2. Check the address carefully, 3. Understand the handling fee, 4. Pay attention to the account time, 5. Confirm that the exchange supports this currency, 6. Pay attention to the minimum withdrawal amount.

Hashbeat App: The highest regulated crypto cloud mining platform in 2025 with free Bitcoin mining rewards and daily spending Hashbeat App: The highest regulated crypto cloud mining platform in 2025 with free Bitcoin mining rewards and daily spending Apr 21, 2025 pm 06:21 PM

The most worth investing in 2025: Cloud mining strategy without eyeing the market If you want to invest in cryptocurrencies in 2025 and don’t want to pay attention to market fluctuations all the time, then cloud mining may be your ideal choice. Cloud mining can easily generate Bitcoin and other digital currencies without expensive mining machines and complex settings. A number of new cloud mining platforms have emerged in 2025, making it easier than ever to get started. Whether it is a novice novices or investors who pursue passive income, the following 11 platforms are worth paying attention to. Hashbeat app: a regulated crypto cloud mining platform that provides free Bitcoin mining rewards, daily payments. If you want to invest in low-risk, high-security, stable returns in cryptocurrency in 2025, Hashbeat app

Top 10 digital virtual currency trading app rankings Top 10 digital currency exchange rankings in 2025 Top 10 digital virtual currency trading app rankings Top 10 digital currency exchange rankings in 2025 Apr 22, 2025 pm 02:45 PM

The top ten digital currency exchanges are: 1. OKX, 2. Binance, 3. gate.io, 4. Coinbase, 5. Kraken, 6. Huobi, 7. KuCoin, 8. Bitfinex, 9. Bitstamp, 10. Poloniex. These exchanges are selected based on factors such as transaction volume, user experience and security, and all provide a variety of digital currency trading services and an efficient trading experience.

How to register an account on Ouyi Exchange Ouyi Exchange Registration Tutorial How to register an account on Ouyi Exchange Ouyi Exchange Registration Tutorial Apr 24, 2025 pm 02:06 PM

The steps to register an Ouyi account are as follows: 1. Prepare a valid email or mobile phone number and stabilize the network. 2. Visit Ouyi’s official website. 3. Enter the registration page. 4. Select email or mobile phone number to register and fill in the information. 5. Obtain and fill in the verification code. 6. Agree to the user agreement. 7. Complete registration and log in, carry out KYC and set up security measures.

See all articles