Ask : Warning: Cannot add header information - headers already sent by

Discussion in 'PHP' started by junandya, Nov 1, 2007.

  1. #1
    Hello,..i have a file called publLoaderDetail.php which included to nextPage.php....and when i run the script this error message is appear:

    Warning: Cannot add header information - headers already sent by (output started at c:\apache\htdocs\greenPlaza full version ii\nextpage.php:16) in c:\apache\htdocs\greenPlaza full version ii\cont\publLoaderDetail.php on line 4


    i think it a session problem, and these are the session script of those file:

    nextpage.php:

    <?PHP
    session_start();

    include "greenPlazaConfig/config.php";
    $connection=mysql_connect($host,$user,$pass) or die(mysql_error());
    mysql_select_db($db,$connection);
    ?>


    and publLoaderDetail.php:

    <?PHP
    if (!isset($_SESSION["username"]) || !isset($_SESSION["password"]))
    {
    header ("location:../homepage.php");
    }
    else
    {
    $username=$_SESSION["username"];
    $password=$_SESSION["password"];

    if(!$username || !$password)
    {
    header ("location:../index.php");
    }
    else
    {
    include "conPlazConfig/config.php";
    $connection=mysql_connect($host,$user,$pass) or die(mysql_error());
    mysql_select_db($db,$connection);

    $loginQuery=mysql_query("select * from apprMemberReg where userCp='$username' and activePwd='$password'",$connection) or die (mysql_error());
    $loginRow=mysql_num_rows($loginQuery);
    $rowLogin=mysql_fetch_array($loginQuery);


    if($loginRow=="0")
    {
    $_SESSION=array();
    session_destroy();
    header ("location:../index.php");
    }
    }
    }


    My question is, :
    1. What is the meaning with that error message?
    2. What is the problem with my script?...

    Thank you very very very much
     
    junandya, Nov 1, 2007 IP
  2. TechEvangelist

    TechEvangelist Guest

    Messages:
    919
    Likes Received:
    140
    Best Answers:
    0
    Trophy Points:
    133
    #2
    The message means that you are outputing text somewhere prior to the header command. You cannot output any text at all--not even a single space--prior to creating a header.
     
    TechEvangelist, Nov 1, 2007 IP
  3. 9450184

    9450184 Peon

    Messages:
    30
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You need to put exit; after each header, like:

    header("Location: ../index.php");
    exit;
     
    9450184, Nov 2, 2007 IP
  4. Oli3L

    Oli3L Active Member

    Messages:
    207
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    70
    #4
    try put ob_start(); at the top of the page,
    and ob_flush(); at the end of the page
     
    Oli3L, Nov 2, 2007 IP
  5. phpl33t

    phpl33t Banned

    Messages:
    456
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Exactly, or enable buffering in your php.ini.

    As far as exiting after header redirects, not needed. It redirects and will not get far enough to exit. Unless you are buffering the data, all headers must come before the html output. Even a space before the header can do it. Buffer and you will be fine.
     
    phpl33t, Nov 2, 2007 IP
  6. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #6
    White space could be before the <?php or after the ?> also, you could be calling session_start after you have outputed to the screen. Most of the time it is white space that happens sometimes with ftp uploading of files. It says it is coming from nextpage.php, but since that is not 16 lines long. I would look into the config.php file for the output error.
     
    exodus, Nov 2, 2007 IP
  7. junandya

    junandya Member

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #7
    thanx, for the explaination guys.......its working now
     
    junandya, Nov 14, 2007 IP