45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:在PHP中检查Email地址的CLASS的方法

在PHP中检查Email地址的CLASS的方法

2016-08-31 07:09:03 来源:www.45fan.com 【

在PHP中检查Email地址的CLASS的方法

<?

/*

* 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/>";

}

}

 

本文地址:http://www.45fan.com/a/question/70126.html
Tags: PHP 精确 检查
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部