php对Email(邮箱地址)进行强验证实现方法

前端之家收集整理的这篇文章主要介绍了php对Email(邮箱地址)进行强验证实现方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
PHP对Email(邮箱地址)进行强验证的代码感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧!
下面的PHP代码可以对电子邮件地址进行简单验证和强验证,简单验证验证邮件格式和主机是否存在,强验证会连接邮件服务器进行验证,需要比较长时间

/**
 * PHP对Email(邮箱地址)进行强验证的代码
 *
 * @param 
 * @arrange 512-笔记网: 512Pic.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,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()); 
}

/***   来自编程之家 jb51.cc(jb51.cc)   ***/

猜你在找的PHP相关文章