
PHP中精确检查Email地址的CLASS(包括对HOST的连通测试)
/*
* email_validation.php
*
*
*/
class email_validation_class
{
//var $email_regular_expression="^([a-z0-9_] |//- |//.)+@(([a-z0-9_] |//-)+//.)+[a-z]{2,4}$";
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(eregi($this->email_regular_expression,$email)!=0);
return(eregi("^([a-z0-9_] |//- |//.)+@(([a-z0-9_] |//-)+//.)+[a-z]{2,4}$",$email)!=0);
}
Function ValidateEmailHost($email,$hosts=0)
{
if(!$this->ValidateEmailAddress($email))
return(0);
$user=strtok($email,"@");
$domain=strtok("");
if(GetMXRR($domain,&$hosts,&$weights))
{
$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 "<center><br/>检查电子邮件地址的正确性:<br/>";
$newmail = "test@test.com";
require("email_validation.php3");
$validator=new email_validation_class;
$validator->timeout=10;
if(IsSet($newemail) && strcmp($newemail,""))
{
if(($result=$validator->ValidateEmailBox($newemail))<0)
{
echo "不能确定您的信箱是否正确. 您的信箱离这里太远了吧?<br/>";
return;
}
else
{
if(!$result)
{
echo "您输入的信箱地址是不正确的! :)<br/>";
return;
}
else
echo "邮箱合法!<br/>";
}
}
发布日期:2021-10-21 03:38:20
浏览次数:17
分类:技术文章
本文共 3134 字,大约阅读时间需要 10 分钟。
<?/*
* email_validation.php
*
*
*/
class email_validation_class
{
//var $email_regular_expression="^([a-z0-9_] |//- |//.)+@(([a-z0-9_] |//-)+//.)+[a-z]{2,4}$";
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(eregi($this->email_regular_expression,$email)!=0);
return(eregi("^([a-z0-9_] |//- |//.)+@(([a-z0-9_] |//-)+//.)+[a-z]{2,4}$",$email)!=0);
}
Function ValidateEmailHost($email,$hosts=0)
{
if(!$this->ValidateEmailAddress($email))
return(0);
$user=strtok($email,"@");
$domain=strtok("");
if(GetMXRR($domain,&$hosts,&$weights))
{
$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 "<center><br/>检查电子邮件地址的正确性:<br/>";
$newmail = "test@test.com";
require("email_validation.php3");
$validator=new email_validation_class;
$validator->timeout=10;
if(IsSet($newemail) && strcmp($newemail,""))
{
if(($result=$validator->ValidateEmailBox($newemail))<0)
{
echo "不能确定您的信箱是否正确. 您的信箱离这里太远了吧?<br/>";
return;
}
else
{
if(!$result)
{
echo "您输入的信箱地址是不正确的! :)<br/>";
return;
}
else
echo "邮箱合法!<br/>";
}
}
转载地址:https://blog.csdn.net/WapWeb/article/details/29445 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2023年03月23日 09时08分56秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
最新文章
Swift中的协议
2020-01-13 08:27:07
Swift-泛型
2020-01-13 08:27:07
iOS自定义控件一
2020-01-13 08:27:07
CALayer相关文章
2020-01-13 08:27:07
CALayer基础
2020-01-13 08:27:07
iOS自定义控件二
2020-01-13 08:27:08
UILabel&UITextView文本嵌入图片处理
2020-01-13 08:27:08
NSAttributedString
2020-01-13 08:27:08
如何处理文字中的emoji?
2020-01-13 08:27:08
Text Programming Guide for iOS
2020-01-13 08:27:08
iOS第三方
2020-01-13 08:27:03
Git使用
2020-01-13 08:27:03
UIWebView
2020-01-13 08:27:03
如何使用Github?
2020-01-13 08:27:03
iOS简单手势解锁
2020-01-13 08:27:04
iOS Apps间分享数据
2020-01-13 08:27:04
iOS事件响应链介绍
2020-01-13 08:27:04
iOS开源项目学习——SVProgressHUD
2020-01-13 08:27:04
Beginning iOS Animation Series (Swift 2)
2020-01-13 08:27:04
iOS9下代码创建约束
2020-01-13 08:27:05