PHP中精确检查Email地址的CLASS(包括对HOST的连通测试)
来源:互联网 发布:淘宝买发票靠谱吗 编辑:程序博客网 时间:2024/04/28 18:29
<?
/*
* 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/>";
}
}
/*
* 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/>";
}
}
- PHP中精确检查Email地址的CLASS(包括对HOST的连通测试)
- JavaScript 检查 email 地址的正则表达式
- php中检查email完整性
- PHP 中检查或过滤IP地址的实现代码
- 获取网页中所有的email地址
- 检查Email地址
- 网站的email地址
- vxWorks中对时间的精确统计
- vxWorks中对时间的精确统计
- 12周(二)1、利用String类或StringBuffer类的方法,对输入的Email地址进行有效性验证。提示: 1)Email地址中应包含“@”和“.”符号; 2)“@”符号应
- 利用String类或StringBuffer类的方法,对输入的Email地址进行有效性验证。提示: 1)Email地址中应包含“@”和“.”符号; 2)“@”符号应该在“.”符号之前; 3
- 1、利用String类或StringBuffer类的方法,对输入的Email地址进行有效性验证。提示:1)Email地址中应包含“@”和“.”符号;2)“@”
- 利用String类或StringBuffer类的方法,对输入的Email地址进行有效性验证。提示: 1)Email地址中应包含“@”和“.”符号; 2)“@”符号应该在“.”符号之前; 3
- ThinkPHP3.2.3完整版中对Auth.class.php的使用
- ThinkPHP3.2.3完整版中对Auth.class.php的使用
- 用PHP校验EMAIL地址的正确方法
- php自带函数判断是否正确的email地址
- C#中检查网络是否连通的二种方法
- PHP 一些小问题~!
- PHP中对文本文件操作很好的类
- PHP中处理截取汉字,出现乱码问题的2个类
- 起始的文字
- PHP中删除文件夹中含有部分字串的文件的解决方法
- PHP中精确检查Email地址的CLASS(包括对HOST的连通测试)
- 完善你的Blog功能(一)
- 让PHP执行一些自己输入的脚本
- Blog 技巧之一:给自己的BLOG来点音乐如何?
- php将mysql中jpeg图片取出后直接缩小然后传给客户端,减轻网络传输负担
- 我选择了WIN32 ASM和 WIN32 SDK
- PHP把汉字转化成unicode码
- PHP中繁体中文转换成简体中文
- 为什么是标准模板库和泛型设计(1)?