免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3694 | 回复: 3
打印 上一主题 下一主题

php验证email是否真实存在 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-08-23 17:30 |只看该作者 |正序浏览
本帖最后由 sosqrn975 于 2014-10-24 10:04 编辑

大家好,我是php新手,现在有个问题想请教大家,就是注册时验证该email地址是否真的存在,谢谢

环境win32+php5.45

test.php


<?php
echo "<center><br/>检查电子邮件地址的正确性:<br/>";
require("email_validation.php";
$newmail = "aa@qq.com";
$validator=new email_validation_class;
$validator->timeout=10;
//if(IsSet($newemail) && strcmp($newemail,""){
if( ($result=$validator->ValidateEmailBox($newmail) )<0){
  echo "不能确定您的信箱是否正确. 您的信箱离这里太远了吧?<br/>";
  return;
}else{
   echo "22222222";
  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="";
var $hosts=0;
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);
//origin:
//return(eregi("^([a-z0-9_] |//- |//.)+@(([a-z0-9_] |//-)+//.)+[a-z]{2,4}$",$email)!=0);
//return preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $_REQUEST[$email]);
return preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $_REQUEST[$email]);
}
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->utLine($connection,"HELO $localhost") && $this->VerifyResultLines($connection,"250")>0 && $this->utLine($connection,"MAIL FROM: <[url=mailtolocaluser@$localhost]$localuser@$localhost[/url]>") && $this->VerifyResultLines($connection,"250")>0 && $this->utLine($connection,"RCPT TO: <$email>") && ($result=$this->VerifyResultLines($connection,"250"))>=0)
{
fclose($connection);
return($result);
}
fclose($connection);
}
}
return(-1);
}
};
?>



test.jpg (36.56 KB, 下载次数: 36)

test.jpg

论坛徽章:
0
4 [报告]
发表于 2012-09-04 11:25 |只看该作者
3楼正解,目前大部分网站都是用发邮件的方式来验证,web设计的原则就是遵循已被验证过的正确的方法。

论坛徽章:
0
3 [报告]
发表于 2012-08-27 09:41 |只看该作者
发一封email过去。里面有验证的链接。

论坛徽章:
0
2 [报告]
发表于 2012-08-25 15:12 |只看该作者
本帖最后由 satrun7 于 2012-08-25 15:13 编辑

现在的新人,在论坛上连代码都不会贴了,一看上面的代码就直接让人晕。
  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP