PHP comes with a method to verify your email address
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
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"));
##
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.
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('bob@example.com', FILTER_VALIDATE_EMAIL)); var_dump(filter_var('http://example.com', 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 evenphp Code to check if a file or directory existsBasics 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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

To safely download the Binance APP, you need to go through the official channels: 1. Visit the Binance official website, 2. Find and click the APP download portal, 3. Choose to scan the QR code, app store, or directly download the APK file to download to ensure that the link and developer information are authentic, and enable two-factor verification to protect the security of the account.

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.

The Ouyi Exchange app supports downloading of Apple mobile phones, visit the official website, click the "Apple Mobile" option, obtain and install it in the App Store, register or log in to conduct cryptocurrency trading.

Registering a Sesame Door Account requires 7 steps: 1. Prepare a valid email or mobile phone number and a stable network; 2. Visit the official website; 3. Enter the registration page; 4. Select and fill in the registration method; 5. Obtain and fill in the verification code; 6. Agree to the user agreement; 7. Complete registration and log in, it is recommended to carry out KYC and set security measures.

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.

Sesame Open Door is a platform that focuses on cryptocurrency trading. Users can obtain portals through official websites or social media to ensure that the authenticity of SSL certificates and website content is verified during access.
