0

I try to add a captcha to this form check the link if you like.

This is the link from Stackoverflow

But i can't make it work correctly when i type the captcha wrong nothing happen, and i still receive the mail.

<?php
session_start();
$cap = 'notEq';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if ($_POST['captcha'] == $_SESSION['cap_code']) {
        // Captcha verification is Correct. Do something here!
        $cap = 'Eq';
    } else {
        // Captcha verification is wrong. Take other action
        $cap = '';
    }



    $to = "exmaple@mail.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $ip = $_POST['ip'];
    $subject = "Form submission";
    $subject2 = "Copy of your form submission";
    $message = $first_name . " " . $last_name . " wrote the following:" . " " . $_POST['message'];
    $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];

    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
    echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
    // You can also use header('Location: thank_you.php'); to redirect to another page.

}

?>

And here below is the captcha and the submit button.

    <!-- Captcha -->
<input type="text" name="captcha" id="captcha" maxlength="6" size="6"/>


    <!-- Submit Button -->
<input type="submit" id="captcha" name="captcha" value="Submit">

So what am i doing wrong in here ?

Thanks for any help.

Community
  • 1
  • 1
Tasos
  • 37
  • 7
  • Where are you running the code that sets the captcha value and stores it on $_SESSION['cap_code'] – RiggsFolly Oct 15 '14 at 20:43
  • @RiggsFolly the script is working, until i added this part from ( $to = "example@mail.com" ) until ( the end php tag ?>) – Tasos Oct 15 '14 at 20:55
  • You could use googles recaptcha instead of trying to create your own? it would make it a lot simpler for you and the number of different capture images will be a lot more than you would probably make personally http://www.google.com/recaptcha/intro/ – akaBase Oct 15 '14 at 20:56

1 Answers1

0

Both your input fields have the same name, so likely the contents of the text field are being overwritten by the value of the submit button.

Change the submit buttons name.

<input type="text" name="captcha" id="captcha" maxlength="6" size="6"/>

<!-- Submit Button -->
<input type="submit" id="captchaBut" name="captchaBut" value="Submit">
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149