May 27, 200917 yr Warning: Cannot modify header information - headers already sent by (output started at /home2/irn3rdc/public_html/test/header.php:12) in /home2/irn3rdc/public_html/test/mailer.php on line 25 this is the error im getting when processing my contact form, I am still recieving the email so it works, but i can figure out what this error is. // delete the cookie so it cannot sent again by refreshing this page setcookie('tntcon',''); } else { The above is line 24 to 26 in mailer.php Is anyone able to help me? Thanks in advance, im gunna keep tinkering to see if i can find out what it is
May 27, 200917 yr You're trying to set a cookie after some output has been sent to the users browser which causes the error. You have to set all cookies, session variables and perform all header modifications before any output is sent to the browser. If you post the code before it we can figure out what is sending the output and you can modify so that it isn't sent until the cookie is set first.
May 27, 200917 yr Author <?php // load the variables $name = $_POST['name']; $subject = $_REQUEST["subject"]; $message = $_REQUEST["message"]; $from = $_REQUEST["email"]; $service = $_REQUEST["service"]; $verif_box = $_REQUEST["verif_box"]; // remove the backslashes that normally appears when entering " or ' $message = stripslashes($message); $subject = stripslashes($subject); $from = stripslashes($from); // check to see if verificaton code was correct if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){ // if verification code was correct send the message and show this page mail("EMAILHERE", 'Online Contact Form From: '. $name, $_SERVER['REMOTE_ADDR']."\n\n"."Subject:"."\n".$subject."\n\n"."Email"."\n".$from."\n\n"."Service"."\n".$service."\n\n"."Message"."\n".$message, "From: $from"); // delete the cookie so it cannot sent again by refreshing this page setcookie('tntcon',''); } else { // if verification code was incorrect then return to contact page and show error header("Location:".$_SERVER['HTTP_REFERER']."?subject=$subject&name=$name&email=$from&message=$message&wrong_code=true"); exit; } ?> <div id="content"> <h2>You're Email has been sent.</h2> <p>Please be patient in waiting for a reply if its an email that requires a reply. Thank you, Ben. </p> This is th whole of the mailer.php, Thanks again for all your help Brightside
May 27, 200917 yr Thanks for the code. It all appears fine, which means that there's probably some thing wrong before this script is even loaded. Your error message says that mailer.php is called from header.php The chance that the mailer.php script is causing an error is unlikely, but just to make sure, can you try commenting out the setcookie line and see if you get any error messages. If you don't then uncomment it, and post header.php here setcookie('tntcon','');
May 27, 200917 yr Author Commenting out the setcookie line works and it doesnt display an error, but so you can have a deeper look into it becuase id like to know what is causing this ill put up whats in my header.php and the code which generates the cookie. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Irnerd Web Design - Ben.C</title> <link href="default.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="container"> <div id="header"> <h1><a href="/">Irn3rd Web-Design</a></h1> <ul class="menu"> <li class="page_item"><a href="/" title="Home">Home</a></li> <li class="page_item"><a href="index.php?view=portfolio" title="Portfolio">The Portfolio</a></li> <li class="page_item"><a href="index.php?view=services" title="Services">Services</a></li> <li class="page_item"><a href="/blog/" title="Blog">Blog</a></li> <li class="page_item"><a href="index.php?view=twitter" title="Twitter">Twitter</a></li> <li class="page_item"><a href="index.php?view=contact" title="Contact Me">Contact Me</a></li> </ul> </div> The above is header.php <?php header('Content-type: image/jpeg'); $width = 50; $height = 24; $my_image = imagecreatetruecolor($width, $height); imagefill($my_image, 0, 0, 0xFFFFFF); // add noise for ($c = 0; $c < 40; $c++){ $x = rand(0,$width-1); $y = rand(0,$height-1); imagesetpixel($my_image, $x, $y, 0x000000); } $x = rand(1,10); $y = rand(1,10); $rand_string = rand(1000,9999); imagestring($my_image, 5, $x, $y, $rand_string, 0x000000); setcookie('tntcon',(md5($rand_string).'a4xn')); imagejpeg($my_image); imagedestroy($my_image); ?> The above is the imageverification generator. Thank you again.
May 27, 200917 yr The header.php is your problem file then, it sends the HTML to the web browser, and then your mailer.php script tries to set a cookie and the result is the error message - remember the restriction that cookie and session variables can't be set, and header information can't be modified, once any output is sent to the browser. I'm assuming that the image generation is done before the header.php file is loaded, which is why the setcookie line in there doesn't cause any problems. What you might be better off doing is buffering the page content, so that nothing is sent until all the page content is generated first. This should actually improve your page performance slightly, because it stores up the contents of the page until everything is ready before sending content to the browser. Also, because nothing is sent to the browser until the end, you can set cookie variables anywhere in your code. Explanation Normally, the PHP page sends bits of data at a time and this is slower than using the buffer. For example, when you include header.php, it'll send the contents of header.php to the browser, and then move on to the next bit, which might be processing your form, sends the results of that, and then goes to footer.php etc... The buffer method will gather all the content from the various files and processes, and then send it to the browser all in one go. Read more here Here's how to do it, I guess you're using the include method I described yesterday, if so edit your index.php file Insert, before you include header.php ob_start(); Insert, after you include footer.php ob_end_flush(); If your footer is static and it's content doesn't depend on any of the page content, then you can place it before you include footer.php too.
May 27, 200917 yr Author Sir, you are a god send, It is now working with the setcookie in my mailer.php file uncommented. Thank you so much for the help over the past two days
May 27, 200917 yr Sir, you are a god send, It is now working with the setcookie in my mailer.php file uncommented. Thank you so much for the help over the past two days Brilliant, I'm glad I could help so much
Create an account or sign in to comment