Hello
I'm using files I found at
HTML contact form with captcha to build a contact form for my site. I'm trying to have the form and the handling PHP in the same file using "if (isset ($_POST['submit']))" to check if the submit button has been pressed or not. The following is the form I'm using as a test:
Code:
<?php
if (isset ($_POST['submit'])) {
session_start();
if( $_SESSION['6_letters_code'] == $_POST['6_letters_code'] && !empty($_SESSION['6_letters_code'] ) ) {
$to = "email@domain.org";
$department = $_POST['department'];
$email_subject = "To:" . $department;
$name = $_POST['name'];
$email = $_POST['email'];
$headers = "From:" . $email;
$message = $_POST['message'];
$email_body = "You have received a new message from " . $name . ".<br />" . $message;
mail($to, $email_subject, $email_body, $headers);
echo 'You entered the correct code. Your message is successfully emailed.';
}
else {
echo "Sorry, you have provided an invalid security code. Please <a href='contact_form_with_captcha.html'>CLICK HERE</a> to try again.";
}
}
else {
print "<form method='POST' name='contact_form' action='contact.php'>
Enter Name: <input type='text' name='name'><br>
Enter Message: <textarea name='message'></textarea><br>
<img src='captcha_code_file.php' /><br>
Enter captcha Code Here :<input id='6_letters_code' name='6_letters_code' type='text' ><br>
<input type='submit' name='submit' value='Submit'><br>
</form>";
}
?>
The problem I'm having is that an error occurs:
Code:
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/ferad123/public_html/contact/contact.php:4) in /home/ferad123/public_html/contact/contact.php on line 6
You entered the correct code. Your message is successfully emailed.
Can anyone help me get the form to work without the "headers already sent" error occurring?
Just in case, here's the code in the captcha_code_file.php
Code:
session_start();
$image_width = 120;
$image_height = 40;
$characters_on_image = 6;
$font = 'monofont.ttf';
$possible_letters = '23456789bcdfghjkmnpqrstvwxyz';
$code = '';
$i = 0;
while ($i < $characters_on_image) {
$code .= substr($possible_letters, mt_rand(0, strlen($possible_letters)-1), 1);
$i++;
}
$font_size = $image_height * 0.75;
$image = @imagecreate($image_width, $image_height);
/* setting the background, text and noise colours here */
$background_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 20, 40, 100);
$image_noise_color = imagecolorallocate($image, 100, 120, 180);
/* generating the dots randomly in background */
for( $i=0; $i<($image_width*$image_height)/3; $i++ ) {
imagefilledellipse($image, mt_rand(0,$image_width), mt_rand(0,$image_height), 1, 1, $image_noise_color);
}
/* generating lines randomly in background of image */
for( $i=0; $i<($image_width*$image_height)/150; $i++ ) {
imageline($image, mt_rand(0,$image_width), mt_rand(0,$image_height), mt_rand(0,$image_width), mt_rand(0,$image_height), $image_noise_color);
}
/* create a text box and add 6 letters code in it */
$textbox = imagettfbbox($font_size, 0, $font, $code);
$x = ($image_width - $textbox[4])/2;
$y = ($image_height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);
/* Show captcha image in the page html page */
header('Content-Type: image/jpeg');// defining the image type to be shown in browser widow
imagejpeg($image);//showing the image
imagedestroy($image);//destroying the image instance
$_SESSION['6_letters_code'] = $code;
Many thanks