打印

[PHP教程] 全面測試email的有效性

全面測試email的有效性

一般我們常希望拜訪你的網站的朋友能留下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 "無效";  



TOP

返回顶部
AYBlue

Processed in 0.040668 second(s), 7 queries.

当前时区 GMT+8, 现在时间是 2009-1-8 11:33 京ICP备06054220号

清除 Cookies - 联系我们 - 163K.com - Archiver - WAP