我为客户制作了一个 PHP 联系表单,他们回复我说他们收到了这样的空白电子邮件:
From:
E-Mail:
Message:
我已经设置了 JavaScript 验证,当我以标准方式提交表单时,它成功拒绝了空白表单。
- 可能发生什么情况?我们在谈论恶意或愚蠢的事情吗?
- 我应该进行哪些额外检查? PHP 检查是标准的事情吗?
代码如下。预先感谢:)
JS 验证:
$("form#submit").submit(function() {
var custname = $('#custname').attr('value');
var custemail = $('#custemail').attr('value');
var custmessage = $('#custmessage').attr('value');
if (custname==null || custname=="" || custemail==null || custemail=="" || custmessage==null || custmessage=="") {
alert("Please fill out the whole form");
return false;
}
PHP 邮件页面:
<?php
$to = "name@email.com";
$subject = "Message from website";
$name_field = htmlspecialchars(trim($_POST['custname']));
$email_field = htmlspecialchars(trim($_POST['custemail']));
$message = htmlspecialchars(trim($_POST['custmessage']));
$body = "From: $name_field\n E-Mail: $email_field\n Message: $message";
mail($to, $subject, $body);
?>
最佳答案
任何发生在客户端(Javascript)的验证都可以轻松绕过,例如使用 FireBug 检查您的页面。
验证仅在客户端用于 UX(用户体验),以使用户的生活更轻松、更快。因此,就真正的验证/安全性而言,我们需要在服务器端(PHP)再次执行此操作。
此外,当前的 PHP 代码打开为 'E-mail Injection' 。您可以检查此问题以确保其安全 Escape string to use in mail() .
关于php - 联系表单验证问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8114775/