轉自 http://southmaster.com/article/pub.php?page=red2.php&id=3891
驗證email是否正確 PHP有內建filter_var()去判斷, 但是天殺的他要另外安裝"Filter"模組才能用
否則會出現Call to undefined function filter_var()
還是用土法煉鋼的方法比較保險 = =
2013/09/23更新
這方法如果domain主機沒有回應會一直跳出PHP WARNING = =
所以又換了一個新方法!
轉自 http://snowtao-li.blogspot.tw/2009/04/smtpemail.html
--
判断一个Email是否存在的类
作者:mlemos
来源:www.fastboard.org
<?php
/**
* @desc SMTP判断邮箱是否存在,检查email地址是否真实存在
原理解释:
在以往的编程中,比如编写用户的资料时,有时需要确认用户输入的Email是否真实有效,以前我们最多只能做到验证Email是否包含了某些特殊的字符,比如"@",".",".com"等,做到的只是判断了Email的合法性,证明用户填写的Email格式是正确的,但是这个Email是否真正的存在于网络中,则没有办法。
首先需要大家了解一下SMTP协议。
1.SMTP是工作在两种情况下:一是电子邮件从客户机传输到服务器;二是从某一个服务器传输到另一个
服务器
2.SMTP是个请求/响应协议,命令和响应都是基于ASCII文本,并以CR和LF符结束。响应包括一个表示返
回状态的三位数字代码
3.SMTP在TCP协议25号端口监听连接请求
4.连接和发送过程
SMTP协议说复杂也不复杂(明明带有“简单”这个词嘛),说简单如果你懂得Sock。不过现在只是我们利用的就是第一条中说的,从客户机传输到服务器,当我们向一台服务器发送邮件时,邮件服务器会首先验证邮件发送地址是否真的存在于本服务器上。
操作的步骤如下:
连接服务器的25端口(如果没有邮件服务,连了也是白连)
发送helo问候
发送mail from命令,如果返回250表示正确可以,连接本服务器,否则则表示服务器需要发送人验证。
发送rcpt to命令,如果返回250表示则Email存在
发送quit命令,退出连接
*/
class email_validation_class {
var $timeout = 0;
var $localhost = "";
var $localuser = "";
function GetLine($connection) {
for($line = "";;) {
if(feof($connection)) {
return(0);
}
$line .= fgets($connection,100);
$length = strlen($line);
if ($length >= 2 && substr($line,$length-2,2) == "\r\n") {
return(substr($line,0,$length-2));
}
}
}
function PutLine($connection,$line) {
return(fputs($connection,"$line\r\n"));
}
function ValidateEmailAddress($email) {
return (preg_match("|^[-_.0-9a-z]+@([-_0-9a-z][-_0-9a-z]+\.)+[a-z]{2,3}$|i",$email));
}
function ValidateEmailHost($email,$hosts = 0) {
if(!$this->ValidateEmailAddress($email)) {
return(0);
}
$user = strtok($email,"@");
$domain = strtok("");
if(GetMXRR($domain,&$hosts,&$weights)) {
// -- GetMXRR only used on unix
$mxhosts = array();
for($host = 0;$host<count($hosts);$host++)
$mxhosts[$weights[$host]] = $hosts[$host];
KSort($mxhosts);
for(Reset($mxhosts),$host = 0;$host<count($mxhosts);Next($mxhosts),$host++)
$hosts[$host] = $mxhosts[Key($mxhosts)];
} else {
$hosts = array();
if(strcmp(@gethostbyname($domain),$domain) != 0)
$hosts[] = $domain;
}
return(count($hosts) != 0);
}
function VerifyResultLines($connection,$code) {
while(($line = $this->GetLine($connection))) {
if (!strcmp(strtok($line," "),$code)) {
return(1);
}
if (strcmp(strtok($line,"-"),$code)) {
return(0);
}
}
return(-1);
}
function ValidateEmailBox($email) {
if(!$this->ValidateEmailHost($email,&$hosts)) {
return(0);
}
if(!strcmp($localhost = $this->localhost,"") && !strcmp($localhost = getenv("SERVER_NAME"),"") && !strcmp($localhost = getenv("HOST"),"")) {
$localhost = "localhost";
}
if(!strcmp($localuser = $this->localuser,"") && !strcmp($localuser = getenv("USERNAME"),"") && !strcmp($localuser = getenv("USER"),"")) {
$localuser = "root";
}
for($host = 0;$host<count($hosts);$host++) {
if(($connection = ($this->timeout ? fsockopen($hosts[$host],25,&$errno,&$error,$this->timeout) : fsockopen($hosts[$host],25)))) {
if($this->VerifyResultLines($connection,"220")>0 && $this->PutLine($connection,"HELO $localhost") && $this->VerifyResultLines($connection,"250")>0 && $this->PutLine($connection,"MAIL FROM: <$localuser@$localhost>") && $this->VerifyResultLines($connection,"250")>0 && $this->PutLine($connection,"RCPT TO: <$email>") && ($result = $this->VerifyResultLines($connection,"250"))>= 0) {
fclose($connection);
return($result);
}
fclose($connection);
}
}
return(-1);
}
};
echo "检查电子邮件地址的正确性:";
$newemail = "snowtao.li@live.com";
$validator = new email_validation_class;
$validator->timeout = 1;
if(IsSet($newemail) && strcmp($newemail,"")) {
if(($result = $validator->ValidateEmailBox($newemail))<0) {
echo "不能确定您的信箱是否正确. 您的信箱离这里太远了吧?";
return;
} else {
if(!$result) {
echo "您输入的信箱地址是不正确的! :)";
return;
} else {
echo "邮箱合法!";
}
}
} else {
echo '郵箱地址錯誤';
}
--
類型:php_article
用PHP來驗證Email是否正確<font cbor=#ff0000> 有什麼問題請與我聯繫:http://www.South Master web@South Master
</font> 轉載請註明出處
當你在某個論壇上註冊時,通常都有一個 e-mail 地址驗證的功能,當你輸入非法的一個格式時會出現某種錯誤提示信息的。
我們可以使用下面的規則表達式
ereg("^[a-za-z0-9_]+@[a-za-z0-9\-]+\.[a-za-z0-9\-\.]+$]", $bail);
但是上面這個式子的功能是只能檢查字符串,不能進行輸出。我們可以進一步利用這個式子來達到返回信息的功能:
if (eregi("^[a-za-z0-9_]+@[a-za-z0-9\-]+\.[a-za-z0-9\-\.]+$]", $bail))
{
return false;
}
下面我們可以進一步來檢測主機名,是不是存在:
list($username, $domain) = split("@",$bail);
if(getmxrr($domain, $mxhost))
{
return true;
}
else
{
if(fsockopen($domain, 25, $errno, $errstr, 30))
{
return true;
}
else
{
return false;
}
}
現在我們再把上面的兩個功能用php組織起來構成一個函數:
function checkemail($bail)
{ if(eregi("^[a-za-z0-9_]+@[a-za-z0-9\-]+\.[a-za-z0-9\-\.]+$]", $bail))
{
return false;
}
list($username, $domain) = split("@",$bail);
if(getmxrr($domain, $mxhost))
{
return true;
}
else
{
if(fsockopen($domain, 25, $errno, $errstr, 30))
{
return true;
}
else
{
return false;
}
}
}
之後我們就可以利用這個函數來檢測是否存在輸入的一個email了,舉個例子:
if(checkemail(web@South Master) == false)
{
echo "您輸入的e_mail是不正確的.";
}
else
{
echo "輸入的e_mail是正確的.";
}
</font> 轉載請註明出處
當你在某個論壇上註冊時,通常都有一個 e-mail 地址驗證的功能,當你輸入非法的一個格式時會出現某種錯誤提示信息的。
我們可以使用下面的規則表達式
ereg("^[a-za-z0-9_]+@[a-za-z0-9\-]+\.[a-za-z0-9\-\.]+$]", $bail);
但是上面這個式子的功能是只能檢查字符串,不能進行輸出。我們可以進一步利用這個式子來達到返回信息的功能:
if (eregi("^[a-za-z0-9_]+@[a-za-z0-9\-]+\.[a-za-z0-9\-\.]+$]", $bail))
{
return false;
}
下面我們可以進一步來檢測主機名,是不是存在:
list($username, $domain) = split("@",$bail);
if(getmxrr($domain, $mxhost))
{
return true;
}
else
{
if(fsockopen($domain, 25, $errno, $errstr, 30))
{
return true;
}
else
{
return false;
}
}
現在我們再把上面的兩個功能用php組織起來構成一個函數:
function checkemail($bail)
{ if(eregi("^[a-za-z0-9_]+@[a-za-z0-9\-]+\.[a-za-z0-9\-\.]+$]", $bail))
{
return false;
}
list($username, $domain) = split("@",$bail);
if(getmxrr($domain, $mxhost))
{
return true;
}
else
{
if(fsockopen($domain, 25, $errno, $errstr, 30))
{
return true;
}
else
{
return false;
}
}
}
之後我們就可以利用這個函數來檢測是否存在輸入的一個email了,舉個例子:
if(checkemail(web@South Master) == false)
{
echo "您輸入的e_mail是不正確的.";
}
else
{
echo "輸入的e_mail是正確的.";
}
--
文章標籤
全站熱搜
