验证码干扰表单处理程序

时间:2012-05-17 19:22:56

标签: php warnings recaptcha contact-form

我正在尝试使用reCaptcha将captcha添加到我的某个网站上的联系表单中。 验证码工作得很好。 联系表格工作正常,没有验证码 - 但是当我添加代码来验证验证码时,它会导致问题。

它给我以下错误消息:

  

警告:无法修改标头信息 - 已发送的标头   (输出始于   /home/liveoutl/domains/mydomainsample.com/public_html/bids/verify.php:1)   在   /home/liveoutl/domains/mydomainsample.com/public_html/bids/verify.php   第53行

听起来像是标题被干扰了 - 但是本身没有标题。 我正在尝试在提交表单时加载“谢谢”页面。 这一直与联系表单处理程序脚本一起使用,但现在验证码验证脚本导致了问题。

有没有更好的方法来加载感谢页?

应注意以下事项:  我对PHP不太熟悉 我正在使用iframe将“contact-form.php”嵌入到实际网站中

这是我在verify.php中的完整代码:

<!-- begin code for including captcha validation-->    
<?php
      require_once('recaptchalib.php');
      $privatekey = "private_key";
      $resp = recaptcha_check_answer ($privatekey,
                                    $_SERVER["REMOTE_ADDR"],
                                    $_POST["recaptcha_challenge_field"],
                                    $_POST["recaptcha_response_field"]);

      if (!$resp->is_valid) {
        // What happens when the CAPTCHA was entered incorrectly
        die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
             "(reCAPTCHA said: " . $resp->error . ")");
      } else {
        // Your code here to handle a successful verification
      }
      ?>
      <!-- from here down handles the actual processing and sending of the form-->
      <?php 
    $errors = '';
    $myemail = 'name@domain.com';//<-----Put Your email address here.
    if(empty($_POST['name'])  || 
       empty($_POST['email']) ||
       empty($_POST['phone']) ||
       empty($_POST['message']))
    {
        $errors .= "\n Error: all fields are required";
    }

    $name = $_POST['name']; 
    $email_address = $_POST['email']; 
    $phone_number = $_POST['phone'];
    $message = $_POST['message']; 

    if (!preg_match(
    "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
    $email_address))
    {
        $errors .= "\n Error: Invalid email address";
    }

    if( empty($errors))
    {
        $to = $myemail; 
        $email_subject = "Contact form submission: $name";
        $email_body = "You have received a new message. ".
        " Here are the details:\n Name: $name \n Email: $email_address \n Phone: $phone_number \n Message \n $message"; 

        $headers = "From: $myemail\n"; 
        $headers .= "Reply-To: $email_address";

        mail($to,$email_subject,$email_body,$headers);
        //redirect to the 'thank you' page
        header('Location: contact-form-thank-you.html');
    } 
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
    <html>
    <head>
        <title>Contact form handler</title>
    </head>

    <body>
    <!-- This page is displayed only if there is some error -->
    <?php
    echo nl2br($errors);
    ?>


    </body>
    </html>

2 个答案:

答案 0 :(得分:1)

您尝试在verify.php中发送HTTP标头:

header('Location: contact-form-thank-you.html');

在第一行发送HTML评论后,被视为回复,从现在开始发送任何标题都会发出此类警告。

如果您确实需要该评论,请将其放在<!DOCTYPE之前或<body>标记中。

答案 1 :(得分:0)

尝试删除评论后的空格

<!-- begin code for including captcha validation-->    
                                                   ____
                                            ^^ Spaces here ^^

如果没有解决,您需要删除评论,并且可以放在<?php标记内:

verify.php

<?php
// begin code for including captcha validation

[...] // Lots of code