PHP自带方法验证邮箱是否存在,php自带验证邮箱
PHP自带方法验证邮箱是否存在,php自带验证邮箱
PHP校验邮箱地址的方法很多, 比较常用的就是自己写正则了, 不过正则多麻烦, 我PHP自带了方法做校验。
filter_var
filter_var是PHP内置的一个变量过滤的方法, 提供了很多实用的过滤器, 可以用来校验整数、浮点数、邮箱、URL、MAC地址等。
filter_var如果返回false, 说明变量无法通过过滤器, 也就是不合法了。
$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));
输出:
string(21) "lastchiliarch@163.com" bool(false) string(7) 1@a.com
对于asb这种非法邮箱格式返回了false, 但对于1@a.com则通过了,还是略有瑕疵啊。
不过一般的正则也通过会认为1@a.com是一个合法的邮箱, 那有啥办法可以更精准的验证呢?
checkdnsrr
checkdnsrr其实是用来查询指定的主机的DNS记录的,我们可以借用它来验证邮箱是否存在。
对于1@a.com肯定是MX记录不存在的。
$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)
可以看到, 很完美, 唯一的缺点就是太慢了, 毕竟是要做一次网络请求。 所以不适合同步对大量的邮箱采用这种做法去校验。
filter_var+checkdnsrr
我们可以接合filter_var 和checkdnsrr做校验, 对于绝大多数的非法邮箱肯定会在filter_var的时候就挂掉了, 剩下的再用
checkdnsrr进一步判断。
$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; } }
输出:
invalid email: 1@a.com
但要注意的是, 由于只是检查MX记录, 所以只能判断163.com是存在的, 但不能说明lastchiliarch这个用户是存在的。
想要更精确的判断邮箱存在, 那只能连接到smtp服务器去验证了。
介绍了邮箱验证,PHP自带方法如何验证邮箱、URL、IP是否合法,下面为大家介绍:
主要还是使用的是filter_var函数。
语法
filter_var(variable, filter, options)
variable 必需。规定要过滤的变量。
filter 可选。规定要使用的过滤器的 ID。
options 规定包含标志/选项的数组。检查每个过滤器可能的标志和选项。
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)); ?>
以上例程会输出:
string(15) "bob@example.com" bool(false)
以上就是本文的全部内容,希望对大家进行php邮箱验证有所帮助。
您可能感兴趣的文章:
- PHP+Ajax异步通讯实现用户名邮箱验证是否已注册( 2种方法实现)
- php使用filter过滤器验证邮箱 ipv6地址 url验证
- js和php邮箱地址验证的实现方法
- php验证邮箱和ip地址最简单方法汇总
- php邮箱地址正则表达式验证

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

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
