PHP中精确检查Email地址的CLASS(包括对HOST的连通测试)
发布日期:2021-10-21 03:38:20 浏览次数:37 分类:技术文章

本文共 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:让PHP执行一些自己输入的脚本
下一篇:PHP中删除文件夹中含有部分字串的文件的解决方法

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年03月14日 19时36分59秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

南昌工程学院c语言答案,南昌工程学院C语言程序设计基础课件第3讲运算符和表达式... 2019-04-21
python学画画_python学画画(下) 2019-04-21
云栖社区 mysql_【直播结束,已更新回放】PG、MySQL到底哪个好?云栖说这次请来五位专家撕了一下-阿里云开发者社区... 2019-04-21
老男孩mysql 百度云_英语语录:除了你,没人能掌控你的幸福 2019-04-21
mysql驱动多次执行问题_Laravel5.2队列驱动expire参数设置带来的重复执行问题 数据库驱动... 2019-04-21
mysql获取刚新增的数据库_如何取得刚插入数据库的数据的id mysql 2019-04-21
python将10到1递减_(Python)如何将3个递减列表合并成一个递减列表? 2019-04-21
python脚本怎么用来处理数据_长时间运行数据处理python脚本的程序结构 2019-04-21
python转成c 语言_将Python对象转换为C void类型 2019-04-21
resin mysql_Eclipse+resin+mysql 安装及环境配置 2019-04-21
redis的使用 Java_java中使用redis 2019-04-21
java 数组元素位置_Java – 在数组中获取元素位置 2019-04-21
c 泛型与java泛型_C ++和Java中的“泛型”类型之间有什么区别? 2019-04-21
java 返回实体对象_java 封装返回结果实体类 返回结果以及错误信息 2019-04-21
java web 防止sql注入攻击_JavaWeb防注入知识点(一) 2019-04-21
java ssm 异常分类_SSM项目常见的异常与处理提示(一) 2019-04-21
java定义矩形类_Java定义矩形类 2019-04-21
java变量怎么变常量_Java的常量与变量是什么?怎么学习呀? 2019-04-21
java开发招聘试题_客户化开发招聘试题-Java开发.doc 2019-04-21
java jdk win10 1335_win10下安装java jdk,tomcat 2019-04-21