1

I have this error and I've been trying to fix it for ages. It appeared from what appears to be out of nowhere as the page it is on was working fine yesterday.

the error is:

Warning: Cannot modify header information - headers already sent by (output started at /var/www/dev.racommerce.com/includes/header.php:97) in /var/www/dev.racommerce.com/cart.php on line 44

the code in the header.php file on line 97 is:

<?for($i = 0; $i < $count; $i++) 
{?> 
  <li><a class="dropDown" href="<?="http://" . $_SERVER['HTTP_HOST'];?>/products.php?cat=<?=$result[$i]['categoryID']?>"><?=$result[$i]['categoryName']?></a></li> 
<?}?>

and the code in the cart.php file on line 44 is:

header("location: cart.php"); 

I have been researching solutions from other answers such as How to fix "Headers already sent" error in PHP but am struggling to understand them or implement them in my code.

Could someone help me with this please?

Community
  • 1
  • 1
Kevlar
  • 344
  • 1
  • 7
  • 25

3 Answers3

4

You can't put anything in before header like echo or close php tag. You can fix it if you put at the beginning

ob_start();

and in the end

ob_end_flush();

this will put everything into a buffer.

jcubic
  • 61,973
  • 54
  • 229
  • 402
  • Thank you that does indeed fix it for me :) much appreciated. the only confusing thing is that I havnt edited these 2 files today but this error just appeared this morning. Not to worry and thanks again. I'll accept when I can – Kevlar Jun 21 '13 at 11:48
  • @Kevlar Even a simple **whitespace** counts as output. – Funk Forty Niner Jun 21 '13 at 12:36
4

You can't output html before modifying header

use ob_start() at first line and ob_flush at the end line

Ali Akbar Azizi
  • 3,272
  • 3
  • 25
  • 44
0

ob_start isn't a solution for your problem. It's a bad thing to do. Try to write your app without the necessity of set headers after output is echoed. You will write better applications. Enjoy ;) D.

Daniele Vrut
  • 2,835
  • 2
  • 22
  • 32