Click here to get back home

Headers already sent error in conditional block

 HomeNewsGroups | Search | About
 comp.lang.php    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Headers already sent error in conditional block mercedes1954 06-09-2008
Posted by mercedes1954 on June 9, 2008, 8:35 am
Please log in for more thread options
Hi,

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.


if($loginmode=="logout")
        {
        $_SESSION=array();
        if(isset($_COOKIE[session_name()]))
                {
                setcookie(session_name(),'',time() -42000,'/');
                }

        session_destroy();

        header("Location: index.php");
        exit;
        }


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

if($errorMessage=="")
        {
        // Set the $_SESSION variables for this logon

        $_SESSION['loginuser']=$row['username'];
        $_SESSION['loginlevel']=$row['level'];

        header("Location: index.php");
        exit;
        }


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

Posted by Captain Paralytic on June 9, 2008, 9:16 am
Please log in for more thread options
> Hi,
>
> 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.
>
> if($loginmode=3D=3D"logout")
> =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 $_SESSION=3Darray();
> =A0 =A0 =A0 =A0 if(isset($_COOKIE[session_name()]))
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 setcookie(session_name(),'',time() -42000,=
'/');
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
>
> =A0 =A0 =A0 =A0 session_destroy();
>
> =A0 =A0 =A0 =A0 header("Location: index.php");
> =A0 =A0 =A0 =A0 exit;
> =A0 =A0 =A0 =A0 }
>
> // Another bhlock that can't run if the first block has......
>
> if($errorMessage=3D=3D"")
> =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 // Set the $_SESSION variables for this logon
>
> =A0 =A0 =A0 =A0 $_SESSION['loginuser']=3D$row['username'];
> =A0 =A0 =A0 =A0 $_SESSION['loginlevel']=3D$row['level'];
>
> =A0 =A0 =A0 =A0 header("Location: index.php");
> =A0 =A0 =A0 =A0 exit;
> =A0 =A0 =A0 =A0 }
>
> How can I write the php script so that multiple header("Location:
> options are present, or can co-exist with session_destroy() ?

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
session_destroy()?

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".

Posted by mercedes1954 on June 9, 2008, 11:02 am
Please log in for more thread options
>
>
>
>
>
> > Hi,
>
> > 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.
>
> > if($loginmode=3D=3D"logout")
> > =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 $_SESSION=3Darray();
> > =A0 =A0 =A0 =A0 if(isset($_COOKIE[session_name()]))
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 setcookie(session_name(),'',time() -4200=
0,'/');
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
>
> > =A0 =A0 =A0 =A0 session_destroy();
>
> > =A0 =A0 =A0 =A0 header("Location: index.php");
> > =A0 =A0 =A0 =A0 exit;
> > =A0 =A0 =A0 =A0 }
>
> > // Another bhlock that can't run if the first block has......
>
> > if($errorMessage=3D=3D"")
> > =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 // Set the $_SESSION variables for this logon
>
> > =A0 =A0 =A0 =A0 $_SESSION['loginuser']=3D$row['username'];
> > =A0 =A0 =A0 =A0 $_SESSION['loginlevel']=3D$row['level'];
>
> > =A0 =A0 =A0 =A0 header("Location: index.php");
> > =A0 =A0 =A0 =A0 exit;
> > =A0 =A0 =A0 =A0 }
>
> > How can I write the php script so that multiple header("Location:
> > options are present, or can co-exist with session_destroy() ?
>
> 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
> session_destroy()?
>
> 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".- Hide quoted text -
>
> - Show quoted text -


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.

Similar ThreadsPosted
error with sessions and headers being sent, yet nothing but headers have been sent June 2, 2005, 2:01 pm
Help! Session error -- headers already sent... February 12, 2005, 6:37 am
[FAQ] Why am I getting the error message 'Headers already sent'? March 1, 2005, 12:59 pm
CGI and HTTP headers error March 1, 2005, 1:28 pm
SoapClient - fault: "HTTP" - Error Fetching http headers. November 21, 2008, 11:47 am
Headers - returning specified HTTP headers from a POST August 11, 2005, 11:56 am
flock won't non-block at all September 20, 2006, 10:44 pm
Block web page for specified IP February 3, 2008, 2:29 pm
Entering a big string as a block... August 20, 2006, 2:34 pm
I need help to block spam messages November 1, 2007, 4:08 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap