简体   繁体   English

PHP联系表单随机提交

[英]PHP Contact Form Submitting Randomly

I hope I'm missing something pretty basic here but: An empty form is getting submitted randomly, sometimes 3-8 times a day, then none for a few days and so on. 我希望我在这里遗漏了一些非常基本的东西但是:空表格随机提交,有时一天3-8次,几天没有,依此类推。

The empty submits always email with the subject as "[Website Contact Form]." 空提交始终通过电子邮件发送主题为“[网站联系表单]”。 Even though there is no validation in my php, in the html code the subject is chosen from a drop-down menu with the default as "General Enquiry." 尽管我的php中没有验证,但在html代码中,主题是从下拉菜单中选择的,默认为“常规查询”。 Notice in the php code below, there is no way for a human to submit an empty form with the above subject line, that is, it would always be "[Website Contact Form]General Enquiry" if I press submit without entering anything. 请注意,在下面的php代码中,人们无法提交带有上述主题行的空表单,也就是说,如果我在没有输入任何内容的情况下按提交,它将始终是“[网站联系表单]一般查询”。

I have contact.html call this contact.php file: 我有contact.html调用这个contact.php文件:

<?
   $email = 'info@mail.com';
   $mailadd = $_POST['email'];
   $headers = 'From: ' . $_POST['email'] . "\r\n";
   $name = $_POST['name'];
   $subject = '[Website Contact Form] ' . $_POST['subject'];
   $message = 'Message sent from: ' . $name . '. Email: ' . $mailadd . '. Organization: ' . $_POST['company'] . '. Phone: ' . $_POST['phone'] . '. ';
   $message .= 'Message: ';
   $message .= $_POST['message'];

   if (mail($email,$subject,$message, $headers)) {
    echo "<p>Thank You! We'll get back to you shortly.</p>";
   }
   else {
    echo "<p>Error...</p>";
   }
?>

I use this code for many websites, but have never encountered this issue. 我在许多网站上使用此代码,但从未遇到过此问题。 Is there something so obviously wrong with this code that I'm missing? 这段代码有什么明显的错误,我错过了吗? Any help would be greatly appreciated! 任何帮助将不胜感激!

I suspect that you may not be checking that these variables are set before you send the email. 我怀疑您在发送电子邮件之前可能没有检查这些变量是否已设置。 Someone requesting contact.php directly (without any form data) may produce the results you have described. 有人直接请求contact.php(没有任何表单数据)可能会产生您描述的结果。 If this is the case, the following code should work like a charm: 如果是这种情况,以下代码应该像魅力一样:

<?php
    if (isset($_POST['submit']) {
        // form code
    }
    else {
        // The form was not submitted, do nothing
    }
?>

Even if that's not that case, such a simple check is always good practice. 即使不是那种情况,这种简单的检查也总是很好的做法。

Furthermore, you should always validate any user input just as a good habit. 此外,您应该始终将任何用户输入验证为一种好习惯。 You don't want your server flooding your inbox with emails. 您不希望您的服务器充斥电子邮件收件箱。 I suggest using regexs to validate the input provided and possibly use a captcha service (such as ReCaptcha). 我建议使用正则表达式验证提供的输入,并可能使用验证码服务(如ReCaptcha)。

If you've been using this code and it's been working fine then I'd check what variables you changed with this case for example your submit form. 如果您一直在使用此代码并且它一直正常工作,那么我将检查您使用此案例更改了哪些变量,例如您的提交表单。

Try out your form with all common possibilities and see if it works. 尝试使用所有常见可能性的表单,看看它是否有效。 And empty Subject will give your form the subject "[Website Contact Form]". 空主题将为您的表单提供“[网站联系表格]”主题。 Check that your script actually get's the post variables and your form submits the right variables. 检查您的脚本实际上是否为post变量,并且您的表单提交了正确的变量。 Your dropdown might have an option with value of "" and the innerHTML "General Enquiry". 您的下拉列表可能有一个值为“”的选项和innerHTML“常规查询”。 The value is what will get submitted. 该值是将要提交的内容。

It's good to check inputs server-side as well 检查服务器端的输入也很好

<?php 
      if(isset($_POST['subject'],$_POST['email'])){

      }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM