• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

全面測試email的有效性_PHP

php 搞代码 4年前 (2022-01-25) 20次浏览 已收录 0个评论

一般我們常希望拜訪你的網站的朋友能留下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: “)
&& $this->VerifyResultLines($connection,”250″)>0
&& $this->PutLine($connection,”RCPT TO: “)
&& ($result=来&源gao@dai!ma.com搞$代^码%网搞gaodaima代码$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(“[email protected]”,0)) echo “有效”;
else echo “無效”;

//線上檢查是否真的有該Email
if($m->Verify(“[email protected]”,1)) echo “有效”;
else echo “無效”;


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:全面測試email的有效性_PHP

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址