0

I'm trying to add captcha to a contact form on one of my sites using reCaptcha. the captcha works fine. the contact form works fine without the captcha- but when i add the code to validate the captcha it causes a problem.

it gives me the following error message:

Warning: Cannot modify header information - headers already sent by (output started at /home/liveoutl/domains/mydomainsample.com/public_html/bids/verify.php:1) in /home/liveoutl/domains/mydomainsample.com/public_html/bids/verify.php on line 53

it sounds to me like the header is being interfered with - but there is no header per se. I'm trying to make it load a 'thank you' page when the form is submitted. this has always worked with the contact form handler script but now the captcha validation script is causing issues.

is there a better way to load the thank you page?

the following should be noted: I'm not very familiar with PHP I'm using an iframe to embed "contact-form.php" into the actual site

here is my full code in 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>
John Saunders
  • 160,644
  • 26
  • 247
  • 397
timatgetdim
  • 191
  • 3
  • 5
  • 16

2 Answers2

1

You try to send HTTP header in verify.php:

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

after sending HTML comment in first line, which is treated as response, from now on sending any headers will issue such warning.

If you really need that comment, put it before <!DOCTYPE or in <body> tag.

bjauy
  • 929
  • 16
  • 22
  • yes, the problem is in the first line html comment and need to be after header('location – khaled_webdev May 17 '12 at 22:17
  • @EVERYONE, the comment has nothing to do with it. ive removed the html comments entirely and im still getting the same message – timatgetdim May 18 '12 at 16:05
  • @timatgetdim sending any character (it can be whitespace put outside of any `` block) before will issue the warning. If you are sure the file above doesn't have them, check all files you include in it or before. – bjauy May 19 '12 at 10:45
0

Try to remove the spaces after your comment

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

If does not solve, you need to remove the comment, and can put inside <?php tag:

verify.php

<?php
// begin code for including captcha validation

[...] // Lots of code
Gabriel Santos
  • 4,934
  • 2
  • 43
  • 74