Home >Backend Development >PHP Tutorial >Demonstration example of verifying email address in php

Demonstration example of verifying email address in php

WBOY
WBOYOriginal
2016-07-25 08:56:421082browse
This article introduces an example of verifying the email address format in php. Friends in need can refer to it.

In PHP code, we often encounter the situation of verifying whether the email format is correct. This section shares an entry-level reference code to teach you some basic verification methods.

Code:

<html>
<head>
<title>php Email验证的演示示例-bbs.it-home.org</title>
</head>
<body>
<form name="form1" method="post" action="EMailValidation.php">
  <table>
      <tr> 
        <td colspan="2"><b>请输入一个Email地址</div></td>
      </tr>
      <tr> 
        <td width="25%">E-mail</td>
        <td width="75%"> 
           <input type="text" name="email" size="30">
        </td>
      </tr>
      <tr> 
        <td colspan="2"> 
          <input type="submit" name="Submit" value="Submit">
        </td>
      </tr>
 </table>
</form>
</body>
</html>
                           
<!-- EMailValidation.php
<html>
<head>
<title> 验证Email地址 </title>
</head>
<body>
<?php
                                                
  if (!isset($email)){
    die("没有检测到任何的Email地址数据");
  }

  if(empty($email)) {
    die("您输入的内容为空"); 
  }
                                                      
  if ( (strlen($email) < 3) || (strlen($email) > 200)) {
    die("Email长度不符合要求");
  } elseif(!ereg("@",$email)) {
    die("Invalid E-mail address, no @ symbol found");
  } else {
    echo "<b>".$email."</b> is correct in format.<br>";
  }
                                                      
  list($username,$hostname) = split("@",$email);
                                                      
  if ( (empty($username)) or (empty($hostname)) ) {
    die("username or host name section is not valid.");
  }
                                                      
  if(checkdnsrr($hostname)) {
      echo "<b>". $email ."</b> hostname has a valid MX record !<br>";
  } else {
      die("<b>". $email ."</b> hostname does not exist");
  }
?>
<br>
</body>
</html>
-->


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