Home > Backend Development > PHP Tutorial > How to add email verification to PHP forms to increase security

How to add email verification to PHP forms to increase security

PHPz
Release: 2023-06-24 09:56:02
Original
1223 people have browsed it

With the continuous advancement of network technology, website applications are becoming more and more mature, but there are also more and more attacks against programs. Among them, a common attack method is to use vulnerabilities to inject malicious code into the website to obtain sensitive information of the website. In order to strengthen the security of the website, we not only need to strengthen security control in the background, but also need to add a verification mechanism to the front-end form.

This article uses PHP forms to verify email addresses to improve the security of front-end forms. By studying this article, you will learn how to use PHP to write email verification code and apply it to forms.

  1. Construction of HTML form

Before writing PHP code, you first need to build an HTML form. Build a basic form using HTML's

tag and tag.

<form method="post" action="submit.php">
  <label>姓名</label>
  <input type="text" name="name"><br>
  <label>电子邮箱</label>
  <input type="text" name="email"><br>
  <input type="submit" name="submit" value="提交">
</form>
Copy after login
  1. Preparation of PHP code

After the form is submitted, the entered email address is verified. PHP provides the filter_var() function to verify whether the entered email address conforms to the specified format. If the entered email address conforms to the specified format, the correct information will be printed out, otherwise the user will be prompted to enter an incorrect email address format.

<?php
if (isset($_POST['submit'])) {
  $name = $_POST['name'];
  $email = $_POST['email'];

  // 验证电子邮箱格式是否正确
  if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "电子邮箱格式不正确!";
  }
  else {
    echo "姓名:".$name."<br>";
    echo "电子邮箱:".$email."<br>";
  }
}
?>
Copy after login
  1. Writing JavaScript code

In order to make the user experience better, we can use JavaScript code to verify the format of the email when the user leaves the input box. When the user leaves the email input box, verify that the entered email address conforms to the specified format. If the email address format is incorrect, a prompt box will pop up to prompt the user to re-enter it.

<script>
function checkEmail() {
  var email = document.getElementById("email").value;
  var regExp = /^([w_.-+])+@([w-]+.)+([w]{2,10})+$/;
  if (!regExp.test(email)) {
    alert("电子邮箱格式不正确!");
    document.getElementById("email").focus();
    return false;
  }
  return true;
}
</script>
Copy after login
  1. Integrate three codes

The above two pieces of code complete the basic email verification, but in the actual application process, we often need to integrate HTML, PHP, JavaScript code.

<html>
<head>
  <title>电子邮箱验证</title>
  <script>
    function checkEmail() {
      var email = document.getElementById("email").value;
      var regExp = /^([w_.-+])+@([w-]+.)+([w]{2,10})+$/;
      if (!regExp.test(email)) {
        alert("电子邮箱格式不正确!");
        document.getElementById("email").focus();
        return false;
      }
      return true;
    }
  </script>
</head>
<body>
  <form method="post" action="submit.php" onsubmit="return checkEmail()">
    <label>姓名</label>
    <input type="text" name="name"><br>
    <label>电子邮箱</label>
    <input type="text" name="email" id="email"><br>
    <input type="submit" name="submit" value="提交">
  </form>
  <?php
  if (isset($_POST['submit'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];

    // 验证电子邮箱格式是否正确
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      echo "电子邮箱格式不正确!";
    }
    else {
      echo "姓名:" . $name . "<br>";
      echo "电子邮箱:" . $email . "<br>";
    }
  }
  ?>
</body>
</html>
Copy after login

We use the onsubmit event to trigger JavaScript code to complete the format verification of the email address. By integrating the above three pieces of code, we can add email verification to the form to improve front-end security.

Summary:

This article provides a PHP-based email verification method and uses JavaScript code to enhance the user experience. By studying this article, you can learn more about how to add verification mechanisms to forms to add more security to your website. In actual application, we can also improve the verification mechanism according to specific needs.

The above is the detailed content of How to add email verification to PHP forms to increase security. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template