Headers already sent error in conditional block

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I am using header("Location: in two places in my script, but they are
in mutually exclusive blocks i.e if one runs, the other can't.

However, an error is thrown pointing to the session_destroy() line
saying it started output. But it's not being called!

I removed the header() line below session_destroy() but it still gives
the same error so it seems to be session_destroy() existing in the
script even if not called.

        setcookie(session_name(),'',time() -42000,'/');


    header("Location: index.php");

// Another bhlock that can't run if the first block has......

    // Set the $_SESSION variables for this logon


    header("Location: index.php");

How can I write the php script so that multiple header("Location:
options are present, or can co-exist with session_destroy() ?

Re: Headers already sent error in conditional block

Quoted text here. Click to load it

Not that I doubt you or anything, but what precisely stops the second
block from running if the first one has run? Why can't there be
something in $errorMessage just because $loginmode=3D=3D"logout"?

Also, the complete error message will detail 2 lines, the one where
output was started and the one that is trying to send more headers. Is
the second reference to the header() line directly below the

Also, looking at your code in the first block, you seem to check for
the presence of a session by checking for $_COOKIE[session_name()].
However, even if you do not find this cookie, you still invoke
session_destroy(). I would expect thhis to give an error along the
lines of "Trying to destroy uninitialized session..." and this would
certainly be some "output already sent".

Re: Headers already sent error in conditional block

Quoted text here. Click to load it

Thank you for the quick response. My brain tricked me into looking at
line 19 - where session_destroy() is - instead of realising it was
line 19 of a small include file that had 2 blank lines after the ?>

Now how many times did I read that in countless posts before turning
here for help???

Thanks for the other comments, but it was a vastly cut down piece of
code and the original addressed your concerns.

Thanks again for your time.

Site Timeline