Home > Article > Backend Development > How to add email verification to PHP forms to increase security
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.
Before writing PHP code, you first need to build an HTML form. Build a basic form using HTML's ff9c23ada1bcecdd1a0fb5d5a0f18437 tag and d5fd7aea971a85678ba271703566ebfd 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>
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>"; } } ?>
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>
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>
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!