423,850 Members | 1,555 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,850 IT Pros & Developers. It's quick & easy.

Cannot modify header information - headers already sent by (output ...

100+
P: 274
I get following error on page :
Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\desiorb\411.php:9) in C:\xampp\htdocs\desiorb\411.php on line 21
when I set a cookie on first page. Please help me fix it.
here is code fot both pages
411.php
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3. <form action="411process.php" method="post">
  4.     Email adddress: <input type="text" name="email" />
  5.     Cell #: <input type="text" name="cell" />
  6.     <input type="Submit" value = "Subscribe"/>
  7.     <input type="checkbox" name="option_SportsPicks" value="SportsPicks"  checked> SportsPicks
  8.     <input type="checkbox" name="option_StockPicks" value="StockPicks" checked > Stock Picks
  9.     <input type="checkbox" name="option_Wedding" value="Wedding" checked> Wedding
  10.     <input type="checkbox" name="option_Tech" value="Tech" checked> Tech. 
  11.     <input type="checkbox" name="option_Fashion" value="Fashion" checked> Fashion 
  12.     <input type="checkbox" name="option_Events" value="Events" checked> Events 
  13.     <input type="checkbox" name="option_Entertainment" value="Entertainment" checked> Entertainment
  14.     <input type="checkbox" name="option_RealEstate" value="RealEstate" checked> Real Estate 
  15.     <input type="checkbox" name="option_Auto" value="Auto" checked> Auto 
  16.     <input type="checkbox" name="option_Employment" value="Employment" checked> Employment 
  17.     <input type="checkbox" name="option_Cuisine" value="Cuisine" checked> Cuisine <br><br><br>
  18. </form>
  19. Sports
  20. <a href="411Directory.php" type="submit"><img src="buttons/sportspicks.png" alt="Sports Picks" name="sportspicks" id="sportspicks"  onclick="411Process.php" border="0" ></a>
  21. <?php setcookie("id","sportspicks",time()+3600);?>
  22. Auto
  23. <a href="411Directory.php"><img src="buttons/auto.png" alt="auto" name="auto" id="Auto" border="0" ></a><br><br>
  24. Stock Picks
  25. <a href="411Directory.php"><img src="buttons/stockpicks.png" alt="stockpicks" name="stockpicks" id="stockpicks" border="0"></a>
  26. Events
  27. <a href="411Directory.php"><img src="buttons/events.png" alt="events" name="events" id="events" border="0"></a><br><br>
  28. Entertainment
  29. <a href="411Directory.php"><img src="buttons/entertainment.png" alt="entertainment" name="entertainment" id="entertainment" border="0"></a>
  30. Wedding
  31. <a href="411Directory.php"><img src="buttons/wedding.png" alt="wedding" name="wedding" id="wedding" border="0" ></a><br><br>
  32. Fashion
  33. <a href="411Directory.php"><img src="buttons/fashion.png" alt="fashion" name="fashion" id="fashion" border="0" ></a>
  34. Cuisine
  35. <a href="411Directory.php"><img src="buttons/cuisine.png" alt="cuisine" name="cuisine" id="cuisine" border="0"></a><br><br>
  36. Real Estate
  37. <a href="411Directory.php"><img src="buttons/realestate.png" alt="realestate" name="realestate" id="realestate" border="0"></a>
  38. Tech
  39. <a href="411Directory.php"><img src="buttons/tech.png" alt="tech" name="tech" id="tech" border="0"></a><br><br>
  40. Employment
  41. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="lastpage.htm"><img src="buttons/employment.png" alt="employment" name="employment" id="employment" border="0"></a>
  42. </body>
  43. </html>
411Directory.php
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3. <?php
  4. $id=$_COOKIE["id"];
  5. // Print a cookie
  6. echo $_COOKIE["id"];
  7. // A way to view all cookies
  8. print_r($_COOKIE);
  9. ?>
  10. </body>
  11. </html>
this is my second day that I am playing with PHP but ths error screwedup my mind.
Please tell what I should do?
thanks in advance.
Jul 16 '08 #1
Share this Question
Share on Google+
2 Replies


Atli
Expert 5K+
P: 5,058
When you set a cookie PHP must add headers to the HTTP Response.
The problem is that when you start printing content, even it that content is just a single white space before the <?php tag, the headers are sent, at which time PHP is unable to add new headers.

So, that means you must set any headers, including cookies, before you send anything else.

If you absolutely must send some content before setting the cookie, you can use Output buffering to delay the output until you chose to send it.

Somewhat like:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. ob_start(); // Start buffering output
  3.  
  4. echo "This stuff just had to be sent before the cookie!";
  5. setcookie(...);
  6.  
  7. ob_end_flush(); // Release the output to the response
  8. ?>
  9.  
Jul 16 '08 #2

100+
P: 274
Thanks a lot . I am not sending data using cookies any more but it dis help understanding what the reason and solution of the problem is. I went through many solution but I found it useful.
Thanks again
Jul 16 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.