PHP Form + CAPTCHA validation

Hi,
in php I am a complete newbie. I made form with php captcha and php sending to email. I have a problem:

  1. Captcha does not work and can not be changed captcha code
  2. Form can not send on email
  3. As soon not write captcha code, so noerror message is displayed
    Please help in solving the problem.

[php]

<?php $nameErr = $emailErr = $opinionErr = ""; $name = $email = $opinion = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["name"])) { $nameErr = "Must enter the Name!"; } else { $name = test_input($_POST["name"]); if (!preg_match("/^[a-zA-Z ]*$/",$name)) { $nameErr = "Only letters and white space allowed"; } } if (empty($_POST["email"])) { $emailErr = "Must enter the name Email!"; } else { $email = test_input($_POST["email"]); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $emailErr = "Invalid email format"; } } if (empty($_POST["opinion"])) { $opinionErr = "Must enter the name opinion"; } else { $opinion = test_input($_POST["opinion"]); } // Check error messages, if none, then send email... if ($nameErr=="" AND $emailErr=="" AND $opinionErr=="") { $to = "My email address"; $name = $_POST['name']; $from = $_POST['email']; $subject = "Opinion on web page"; $subject2 = "Copy of your form - Opinions on website"; $opinion = $title . " " . $name . " " . "\n\n" . $_POST['comment']; $opinion2 = "Here is a copy - Opinions on website " . $name . "\n\n" . $_POST['comment']; if(empty($_SESSION['6_letters_code'] ) || strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0) { //Note: the captcha code is compared case insensitively. //if you want case sensitive match, update the check above to // strcmp() $errors .= "n The captcha code does not match!"; } $headers = "From:" . $from; $headers2 = "From:" . $to; mail($to,$subject,$opinion,$headers); mail($from,$subject2,$opinion2,$headers2); header("Location: thanks.html"); } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } function IsInjected($str) { $injections = array('(\n+)', '(\r+)', '(\t+)', '(%0A+)', '(%0D+)', '(%08+)', '(%09+)' ); $inject = join('|', $injections); $inject = "/$inject/i"; if(preg_match($inject,$str)) { return true; } else { return false; } } ?>

[/php]

In html I have typed:

<img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' ><br>
<label for='message'>Enter the code above here :</label><br>
<input id="6_letters_code" name="6_letters_code" size='8' type="text" class="code"><br>
<a href='javascript: refreshCaptcha();' style='position:relative;top:-27px;left:100px;'><img src="image/circle.png"></a>

Thank you all for help

Will, cleaning up some old posts, I found this one. Did you fix your errors yet?

There are several issues with your code, but, one large one is that you do not run ALL of your code when the form is posted.
Basically, you have in general terms code flow like this:

IF form is posted { check name }
Here all other code.

So, all other code gets processed no matter if form is posted. You should have it work more like this, in general:

IF form is posted {
check name;
here all other code;
}

You want all of the code you posted to us to run inside of your " if ($_SERVER[“REQUEST_METHOD”] == “POST”) { "
Also, your headers for your email are incorrect. I think you should create a test page just for sending an email first and get it working correctly. Here is a base example of sending an email out using PHP:
[php]
$subject = ‘Important Message from me!’;
$headers = ‘MIME-Version: 1.0’ . “\r\n”;
$headers .= ‘Content-type: text/html; charset=iso-8859-1’ . “\r\n”;
$headers .= ‘Reply-To: [email protected]’ . “\r\n”;
$headers .= ‘From: www.mydomain.com < [email protected] >’ . “\r\n”;

        mail($email, $subject, $message, $headers);

[/php]
This is for sending HTML emails. The reply-to is not needed if you own the domain it is sending from. The MIME and content-type are important for some servers. Not sure if this helps, but, hope so!

Sponsor our Newsletter | Privacy Policy | Terms of Service