Jump to content

Attempting Contact Form W/ Captcha


bukwus

Recommended Posts

HelloI'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:

<?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:

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 6You 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

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 widowimagejpeg($image);//showing the imageimagedestroy($image);//destroying the image instance$_SESSION['6_letters_code'] = $code;

Many thanks

Link to comment
Share on other sites

Thanks for the reply. I'm trying to get my head around sessions.I've been trying a less thorough alternative with the form and PHP on separate pages. I'm finding that if I put just about any other code (including HTML) in the PHP file that processes the form info, it causes the "headers already sent" error. I even stripped it down so there was only an image tag and the PHP and is till returned an error.Form

<form method="POST" name="contact_form" action="contact_engine.php">										<div class="fhFormLeft2">To:</div>																				<div class="fhFormRight2">											<select name="department">												<option value="Donor Services">Donor Services</option>												<option value="Events Coordinator">Events Coordinator</option>												<option value="General Inquiries" selected="selected" >General Inquiries</option>												<option value="Grant Makers">Grant Makers</option>												<option value="Human Resources">Human Resources</option>												<option value="Press Inquiry">Press Inquiry</option>												<option value="Internship">Internship</option>												<option value="Program Information">Program Information</option>												<option value="Volunteer">Volunteer</option>												<option value="Website Feedback">Website Feedback</option>											</select>										</div>																				<div class="fhFormLeft2">*First Name:</div>																				<div class="fhFormRight2">											<input type="text" name="nameFirst">										</div>																				<div class="fhFormLeft2">*Last Name:</div>																				<div class="fhFormRight2">											<input type="text" name="nameLast">										</div>																				<div class="fhFormLeft2">Title:</div>																				<div class="fhFormRight2">											<input type="text" name="title">										</div>																				<div class="fhFormLeft2">*Company Name:</div>																				<div class="fhFormRight2">											<input type="text" name="company">										</div>																				<div class="fhFormLeft2">Address 1:</div>																				<div class="fhFormRight2">											<input type="text" name="address1">										</div>																				<div class="fhFormLeft2">Address 2:</div>																				<div class="fhFormRight2">											<input type="text" name="address2">										</div>																				<div class="fhFormLeft2">City:</div>																				<div class="fhFormRight2">											<input type="text" name="city">										</div>																				<div class="fhFormLeft2">State:</div>																				<div class="fhFormRight2">											<select name="state" size="1">												<option value="AK" selected="selected" >Alaska</option>												<option value="AL">Alabama</option>																								<option value="AR">Arkansas</option>												<option value="AZ">Arizona</option>												<option value="CA">California</option>												<option value="CO">Colorado</option>												<option value="CT">Connecticut</option>												<option value="DC">District of Columbia</option>												<option value="DE">Delaware</option>												<option value="FL">Florida</option>												<option value="GA">Georgia</option>																								<option value="HI">Hawaii</option>												<option value="IA">Iowa</option>												<option value="ID">Idaho</option>												<option value="IL">Illinois</option>												<option value="IN">Indiana</option>												<option value="KS">Kansas</option>												<option value="KY">Kentucky</option>												<option value="LA">Louisiana</option>												<option value="MA">Massachusetts</option>																								<option value="MD">Maryland</option>												<option value="ME">Maine</option>												<option value="MI">Michigan</option>												<option value="MN">Minnesota</option>												<option value="MO">Missouri</option>												<option value="MS">Mississippi</option>												<option value="MT">Montana</option>												<option value="NC">North Carolina</option>												<option value="ND">North Dakota</option>																								<option value="NE">Nebraska</option>												<option value="NH">New Hampshire</option>												<option value="NJ">New Jersey</option>												<option value="NM">New Mexico</option>												<option value="NV">Nevada</option>												<option value="NY">New York</option>												<option value="OH">Ohio</option>												<option value="OK">Oklahoma</option>												<option value="OR">Oregon</option>																								<option value="PA">Pennsylvania</option>												<option value="PR">Puerto Rico</option>												<option value="RI">Rhode Island</option>												<option value="SC">South Carolina</option>												<option value="SD">South Dakota</option>												<option value="TN">Tennessee</option>												<option value="TX">Texas</option>												<option value="UT">Utah</option>												<option value="VA">Virginia</option>																								<option value="VT">Vermont</option>												<option value="WA">Washington</option>												<option value="WI">Wisconsin</option>												<option value="WV">West Virginia</option>												<option value="WY">Wyoming</option>											</select>										</div>																				<div class="fhFormLeft2">Zip/Postal Code:</div>																				<div class="fhFormRight2">											<input type="text" name="zip">										</div>																				<div class="fhFormLeft2">*Business Phone:</div>																				<div class="fhFormRight2">											<input type="text" name="phoneBiz">										</div>																				<div class="fhFormLeft2">*Email:</div>																				<div class="fhFormRight2">											<input type="text" name="email">										</div>																				<div class="fhFormLeft2">*Request or Comments <br />(<i>Limit 1,000 characters</i>)</div>																				<div class="fhFormRight2">											<textarea cols="30" rows="6" name="message"></textarea>										</div>																				<div class="fhFormLeft2">											*Ani-spam Code:										</div>																				<div class="fhFormRight2">											<img src="captcha_code_file.php" /><br />											<input id="6_letters_code" name="6_letters_code" type="text" ><br /><br />											<input type="reset" value="Clear" />											<input type="submit" value="Submit" />										</div>									</form>

PHP without anything else. This returns no errors, but is nothing but plain text on an otherwise blank page.

<?php session_start();//Check for errorsif (empty($_POST['nameFirst'])) {	$problem = TRUE;	$firstNameWarn = TRUE;}if (empty($_POST['nameLast'])) {	$problem = TRUE;	$lastNameWarn = TRUE;}if (empty($_POST['company'])) {	$problem = TRUE;	$companyWarn = TRUE;}if (empty($_POST['phoneBiz'])) {	$problem = TRUE;	$phoneWarn = TRUE;}if (empty($_POST['email'])) {	$problem = TRUE;	$emailWarn = TRUE;}if (empty($_POST['message'])) {	$problem = TRUE;	$messageWarn = TRUE;}if ($_SESSION['6_letters_code'] != $_POST['6_letters_code'] || empty($_SESSION['6_letters_code'])) {	$problem = TRUE;	$codeWarn = TRUE;}if ($problem == TRUE) {	print '<p>Oops. The following information is required to properly process your contact request.<br />';	if ($firstNameWarn == TRUE) {		print 'Please enter a first name.<br />';	}		if ($lastNameWarn == TRUE) {		print 'Please enter a last name.<br />';	}		if ($companyWarn == TRUE) {		print 'Please enter a company name.<br />';	}		if ($phoneWarn == TRUE) {		print 'Please enter a phone number.<br />';	}		if ($emailWarn == TRUE) {		print 'Please enter an email address.<br />';	}		if ($messageWarn == TRUE) {		print 'Please enter a message.<br />';	}		if ($messageWarn == TRUE) {		print 'Please enter a message.<br />';	}		if ($codeWarn == TRUE) {		print 'Please enter the correct security code.<br />';	}		print 'Please use the back button on your browser to return to the form and enter the correct information. Thank you.</p>';}else {//Define variables using data pulled from completed form	$department = $_POST['department'];	$nameFirst = $_POST['nameFirst'];	$nameLast = $_POST['nameLast'];	$name = $nameFirst . ' ' . $nameLast;	$title = $_POST['title'];	$company = $_POST['company'];	$address1 = $_POST['address1'];	$address2 = $_POST['address2'];	$city = $_POST['city'];	$state = $_POST['state'];	$zip = $_POST['zip'];	$phoneBiz = $_POST['phoneBiz'];	$email = $_POST['email'];	$message = $_POST['message'];	//Define variables used in mail function	$to = 'andy@buzgate.org';	$email_subject = 'FERAD Contact: ' . $department;	$email_body = "Name: ".$name."\n	Title: ".$title."\n	Company: ".$company."\n	Address: ".$address1."\n	Address: ".$address2."\n	City: ".$city."\n	State: ".$state."\n	Zip Code: ".$zip."\n	Phone#: ".$phoneBiz."\n	Email: ".$email."\n	Message:<br />	".$message;	$headers = "From:" . $email;	//Place variables in mail funciton	mail($to, $email_subject, $email_body, $headers);	//Response to user after correctly filling out form	echo $nameFirst.'<br />	Your message has been sent. Thank you for contacting FERAD.<br /><br />	Andrew Fling<br />	Webmaster';}?>

There's a description about that error here:http://w3schools.invisionzone.com/index.php?showtopic=12509Basically, you have output being sent at some point before using session_start. You can't send output before you use session_start.
Link to comment
Share on other sites

You need to have session_start before any other output, including HTML output. It's better if you can have all of your PHP code before any HTML code, because it helps to separate the two, but all you really need is the call to session_start first before you send anything to the browser, you can still have PHP code farther down as long as you start the session before you output anything.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...