Tag: headers sent error message

  • Warning: headers already sent

    When I was learning PHP their were few things that had stumped me, I would usually take it hours to solve them if not days. Now I have grown up, and I realize that those were the basic things that should have been mentioned in the PHP manual, but they were not.

    Today I am going to write about one such annoying problem that almost always comes in the way of PHP developers at least once.

    Warning: Cannot modify header information – headers already sent by (output started at C:wampwwwaktesterror.php:2) in C:wampwwwaktesterror.php on line 4

    Warning: session_start() [function.session-start]: Cannot send session cookie – headers already sent by (output started at C:wampwwwaktesterror.php:2) in C:wampwwwaktesterror.php on line 3

    This is “header already sent” warning message, that we get whenever we try to redirect a user to some other page or location, when we try to set a cookie or if we try to start a session. Once we get this warning we know that page won’t redirect, session won’t start or cookie’s sent to user, So we can’t even ignore them.

    Before we see the solutions to resolve this warning let’s see the source code of the error.php file that I used to generate this error.

    
    <?php
    session_start();
    header("location:http://localhost/");
    ?>

    Please notice an empty line before “<?php”, this is the cause of errors in his page.

    (more…)