欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php验证email地址的类(经典)

程序员文章站 2022-03-16 14:00:57
...
分享一个php类,用于验证email地址,一般我们会通过 js的正则来验证,这里分享的代码是用原生php来实现,有需要的朋友,可以参考下。

本文提供的这个php email验证类,具有如下的特点: 1,可以作简单的email地址验证。 2,可以验证email地址是否属于一个有效的邮件交换记录域。 3,可以通过尝试连接邮件服务器,来检测邮箱是否真实存在。 4,对于windows平台,实现了getmxrr不具备的功能,用到了Moriyoshi Koizumi的 DNS 解析类。

代码:

next_token; 
        } 
        for($character=0;$characternext_token=substr($string,$found+1); 
            return(substr($string,0,$found)); 
        } 
        else 
        { 
            $this->next_token=""; 
            return($string); 
        } 
    } 

    Function OutputDebug($message) 
    { 
        $message.="\n"; 
        if($this->html_debug) 
            $message=str_replace("\n","
\n",HtmlEntities($message)); echo $message; flush(); } 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") { $line=substr($line,0,$length-2); if($this->debug) $this->OutputDebug("S $line"); return($line); } } } Function PutLine($connection,$line) { if($this->debug) $this->OutputDebug("C $line"); return(fputs($connection,"$line\r\n")); } Function ValidateEmailAddress($email) { if(IsSet($this->preg)) { if(strlen($this->preg)) return(preg_match($this->preg,$email)); } else { $this->preg=(function_exists("preg_match") ? "/".str_replace("/", "\\/", $this->email_regular_expression)."/" : ""); return($this->ValidateEmailAddress($email)); } return(eregi($this->email_regular_expression,$email)!=0); } Function ValidateEmailHost($email,&$hosts) { if(!$this->ValidateEmailAddress($email)) return(0); $user=$this->Tokenize($email,"@"); $domain=$this->Tokenize(""); $hosts=$weights=array(); $getmxrr=$this->getmxrr; if(function_exists($getmxrr) && $getmxrr($domain,$hosts,$weights)) { $mxhosts=array(); for($host=0;$hostexclude_address)==0 || strcmp(@gethostbyname($this->exclude_address),$ip))) $hosts[]=$domain; } return(count($hosts)!=0); } Function VerifyResultLines($connection,$code) { while(($line=$this->GetLine($connection))) { $this->last_code=$this->Tokenize($line," -"); if(strcmp($this->last_code,$code)) return(0); if(!strcmp(substr($line, strlen($this->last_code), 1)," ")) return(1); } 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;$hostdebug) $this->OutputDebug("Resolving host name \"".$hosts[$host]."\"..."); if(!strcmp($ip=@gethostbyname($domain),$domain)) { if($this->debug) $this->OutputDebug("Could not resolve host name \"".$hosts[$host]."\"."); continue; } } if(strlen($this->exclude_address) && !strcmp(@gethostbyname($this->exclude_address),$ip)) { if($this->debug) $this->OutputDebug("Host address of \"".$hosts[$host]."\" is the exclude address"); continue; } if($this->debug) $this->OutputDebug("Connecting to host address \"".$ip."\"..."); if(($connection=($this->timeout ? @fsockopen($ip,25,$errno,$error,$this->timeout) : @fsockopen($ip,25)))) { $timeout=($this->data_timeout ? $this->data_timeout : $this->timeout); if($timeout && function_exists("socket_set_timeout")) socket_set_timeout($connection,$timeout,0); if($this->debug) $this->OutputDebug("Connected."); 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=$this->VerifyResultLines($connection,"250"))>=0) { if($result) { if($this->PutLine($connection,"DATA")) $result=($this->VerifyResultLines($connection,"354")!=0); } else { if(strlen($this->last_code) && !strcmp($this->last_code[0],"4")) $result=-1; } if($this->debug) $this->OutputDebug("This host states that the address is ".($result ? ($result>0 ? "valid" : "undetermined") : "not valid")."."); fclose($connection); if($this->debug) $this->OutputDebug("Disconnected."); return($result); } if($this->debug) $this->OutputDebug("Unable to validate the address with this host."); fclose($connection); if($this->debug) $this->OutputDebug("Disconnected."); } else { if($this->debug) $this->OutputDebug("Failed."); } } return(-1); } }; ?> //GetMXRR function emulation need to make the class work properly under Windows and other platforms without this function (needs DNS.php mentioned in the Related links section). All Rights Reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ------------------------------------------------------------------------ */ if(IsSet($_NAMESERVERS) && (GetType($_NAMESERVERS)!="array" || count($_NAMESERVERS)==0)) Unset($_NAMESERVERS); /*************************************************************************** Description $_NAMESERVER[] The array that contains IP addresses or domain names of name servers used for DNS resolution. If nothing is set before require()'ing this library, the values will automatically prepared. bool getmxrr( string $hostname, arrayref $mxhosts, arrayref $weight ); This function works in the same way as getmxrr(), however the third parameter cannot be omitted. If you need no MX preference information, please do like: getmxrr( 'example.com', $mxhosts, ${''} ); ------------------------------------------------------------------------- Configuration If you are doing with win32 environments and don't set $_NAMESERVER manually, make sure that ipconfig.exe is within the PATH. ipconfig.exe is generally distributed with any Microsoft(R) Windows distributions except for Windows 95. ***************************************************************************/ require_once( 'DNS.php' ); /* rewrite this path to the same as the box's configuration if you run scripts on *NIX platforms */ define( 'RESOLV_CONF_PATH', '/etc/resolv.conf' ); if( !isset( $_NAMESERVERS ) ) { $_NAMESERVERS = array(); if( strncmp( PHP_OS, "WIN", 3 ) == 0 ) { unset( $res ); exec( 'ipconfig /all', $res ); $cnt = count( $res ); for( $i = 0; $i sendQuery( new DNSQuery( new DNSRecord( $dnsname, DNS_RECORDTYPE_MX ) ) ); if( $answer === false || $answer->rec_answer === false ) { return false; } else { $i = count( $answer->rec_answer ); $mxhosts = $weight = array(); while( --$i >= 0 ) { if( $answer->rec_answer[$i]->type == DNS_RECORDTYPE_MX ) { $rec = &$answer->rec_answer[$i]->specific_fields; $mxhosts[] = substr( $rec['exchange']->getCanonicalName(), 0, -1 ); $weight[] = $rec['preference']; } } return true; } } ?> //TEST SCRIPT Test for Manuel Lemos's PHP E-mail validation class

Test for Manuel Lemos's PHP E-mail validation class


getmxrr="_getmxrr"; } */ /* how many seconds to wait before each attempt to connect to the destination e-mail server */ $validator->timeout=10; /* how many seconds to wait for data exchanged with the server. set to a non zero value if the data timeout will be different than the connection timeout. */ $validator->data_timeout=0; /* user part of the e-mail address of the sending user (info@phpclasses.org in this example) */ $validator->localuser="info"; /* domain part of the e-mail address of the sending user */ $validator->localhost="phpclasses.org"; /* Set to 1 if you want to output of the dialog with the destination mail server */ $validator->debug=1; /* Set to 1 if you want the debug output to be formatted to be displayed properly in a HTML page. */ $validator->html_debug=1; /* When it is not possible to resolve the e-mail address of destination server (MX record) eventually because the domain is invalid, this class tries to resolve the domain address (A record). If it fails, usually the resolver library assumes that could be because the specified domain is just the subdomain part. So, it appends the local default domain and tries to resolve the resulting domain. It may happen that the local DNS has an * for the A record, so any sub-domain is resolved to some local IP address. This prevents the class from figuring if the specified e-mail address domain is valid. To avoid this problem, just specify in this variable the local address that the resolver library would return with gethostbyname() function for invalid global domains that would be confused with valid local domains. Here it can be either the domain name or its IP address. */ $validator->exclude_address=""; if(IsSet($_GET["email"])) $email=$_GET["email"]; if(IsSet($email) && strcmp($email,"")) { if(($result=$validator->ValidateEmailBox($email))
It was not possible to determine if $email is a valid deliverable e-mail box address.
\n"; else echo "

$email is ".($result ? "" : "not ")."a valid deliverable e-mail box address.

\n"; } else { $port=(strcmp($port=getenv("SERVER_PORT"),"") ? intval($port) : 80); $site="http://".(strcmp($site=getenv("SERVER_NAME"),"") ? $site : "localhost").($port==80 ? "" : ":".$port).GetEnv("REQUEST_URI"); echo "

Access this page using a URL like: $site?email=your@test.email.here

\n"; } ?>