会员: 密码:  免费注册 | 忘记密码 | 会员登录 网页功能: 加入收藏 设为首页 网站搜索  
技术文档 > Web开发 > PHP
全面--email的有效性
发表日期:2004-07-21 16:02:09作者: 出处:  

一般我?常希望拜?你的?站的朋友能留下Email  
但是很多人都??便打,造成管理?的困?,  
以下??class可以?上?查Email是否是有效的Email(存不存在)  

<?  
class CEmail {  
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)=="rn")  
return(substr($line,0,$length-2));  
}  
}  

Function PutLine($connection,$line)  
{  
return(fputs($connection,"$linern"));  
}  

Function VerifyRule($email)  
{  
return(eregi($this->email_regular_expression,$email)!=0);  
}  

Function ValidateEmailHost($email,$hosts=0)  
{  
if(!$this->VerifyRule($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 VerifyOnline($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);  
}  

function Verify($email,$type=0) {  
    if($type==0) return $this->VerifyRule($email) ;  
    else     return $this->VerifyOnline($email) ;  

}  

};      
      
      
?>  


用法:  
$m=new CEmail;  
//??查?法  
if($m->Verify("jerry@mail.jerry.com.tw",0)) echo "有效";  
else echo "?效";  

//?上?查是否真的有?Email  
if($m->Verify("jerry@mail.jerry.com.tw",1)) echo "有效";  
else echo "?效";  

 

返回顶部】 【打印本页】 【关闭窗口

关于我们 / 给我留言 / 版权举报 / 意见建议 / 网站编程QQ群   
Copyright ©2003- 2024 Lihuasoft.net webmaster(at)lihuasoft.net 加载时间 0.00577