Home >php教程 >php手册 >php验证用户输入的邮箱有效性和正确性

php验证用户输入的邮箱有效性和正确性

WBOY
WBOYOriginal
2016-06-13 11:33:531603browse

  function validate_email($email){

  $exp="^[a-z'0-9]+([._-][a-z'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$";

  if(eregi($exp,$email)){ //先用正则表达式验证email格式的有效性

  if(checkdnsrr(array_pop(explode("@",$email)),"MX")){//再用checkdnsrr验证email的域名部分的有效性

  return true;

  }else{

  return false;

  }

  }else{

  return false;

  }

  }

  注意:checkdnsrr函数在win主机上是无效的!下面是国外某程序员提出的一种解决办法,另外写了个函数代替checkdnsrr函数:

  function myCheckDNSRR($hostName, $recType=''){

  if(!emptyempty($hostName)){

  if( $recType=='' ) $recType="MX";

  exec("nslookup -type=$recType $hostName", $result);

  foreach($result as $line){

  if(eregi("^$hostName",$line)){

  return true;

  }

  }

  return false;

  }

  return false;

  }

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn