这篇文章主要为大家详细介绍了php对Email(邮箱地址)进行强验证实现方法,具有一定的参考价值,可以用来参考一下。
对php对Email(邮箱地址)进行强验证的代码感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!
/**
* php对Email(邮箱地址)进行强验证的代码
*
* @param
* @arrange 512-笔记网: q1010.com
**/
<?php
/*
* __construct($email) takes an email address to check
*
* simpleCheck() Tests to see if an email address is formatted correctly
* and the domain it belongs to exists, such as: gmail.com, yahoo.com
*
* strongCheck() Tests to see if an email address is valid and that the
* email actually accepts emails by actually connecting to the server.
* Note: strongCheck() can be slow
*/
class EmailValidator{
private $email = "";
private $mxhost = "";
public function __construct($email){
$this->email = $email;
$this->mxhost = $this->getMXHost();
}
public function strongCheck(){
if(filter_var($this->email, FILTER_VALIDATE_EMAIL) && $this->fConnect()){
return true;
}
return false;
}
public function simpleCheck(){
if(filter_var($this->email, FILTER_VALIDATE_EMAIL) && $this->getMXHost()){
return true;
}
return false;
}
private function fConnect(){
$fp = @fsockopen($this->mxhost, 25, $errno, $errstr, 5);
$ms_resp = "";
$b_server_found = false;
if($fp){
$ms_resp .= $this->send_command($fp, "HELO hi");
$ms_resp .= $this->send_command($fp, "MAIL FROM:<ryan@ryannaddy.com>");
$rcpt_text = $this->send_command($fp, "RCPT TO:<{$this->email}>");
$ms_resp .= $rcpt_text;
if(substr($rcpt_text, 0, 3) == "250"){
$b_server_found = true;
}
$ms_resp .= $this->send_command($fp, "QUIT");
fclose($fp);
}
return $b_server_found;
}
private function getMXHost(){
if(!empty($this->mxhost)){
return $this->mxhost;
}
list($user, $domain) = explode("@", $this->email);
getmxrr($domain, $hosts, $weights);
$priority = mt_getrandmax();
$key = 0;
if(empty($weights)){
return false;
}
foreach($weights as $k => $v){
if($v < $priority){
$key = $k;
$priority = $v;
}
}
return $hosts[$key];
}
private function send_command($fp, $out){
fwrite($fp, $out . "\r\n");
return $this->get_data($fp);
}
private function get_data($fp){
$s = "";
stream_set_timeout($fp, 2);
for($i = 0; $i < 2; $i++){
$s .=fgets($fp, 1024);
}
return $s;
}
}
$hosts = array(
"asdfsfd@adsfasdasd.com",
"asdfsfd@google.com",
"asdf!sfd@gmail.com",
"asdfsfd@yahoo.com",
"asd^sfd@44n5o$.com",
"dddasdf@gamil.com"
);
foreach($hosts as $host){
echo $host . "\n";
$em = new EmailValidator($host);
echo " ";
var_dump($em->simpleCheck());
echo " ";
var_dump($em->strongCheck());
}
/*** 来自四海网(www.q1010.com) ***/
本文来自:http://www.q1010.com/173/1107-0.html
注:关于php对Email(邮箱地址)进行强验证实现方法的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:邮箱地址
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。