dcsimg
www.webdeveloper.com
Results 1 to 9 of 9

Thread: session_start(); used with other headers?

  1. #1
    Join Date
    Mar 2010
    Posts
    78

    session_start(); used with other headers?

    Is there a way to use session_start(); at the top of a php page, and still be able to use header statments in the same php page later on? without getting those nasty headers already sent error messages?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,610
    You can use as many different header type statements as you want (session_start(), setcookie(), header()...) as long as all of them come before any output is generated (whether via echo, e.g., or text not within <?php...?> tags). If you find yourself simply unable to organize your script in such a way to avoid any output, you can use the output buffering functions, but this may add a bit of overhead to things.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Mar 2010
    Posts
    78
    Quote Originally Posted by NogDog View Post
    You can use as many different header type statements as you want (session_start(), setcookie(), header()...) as long as all of them come before any output is generated (whether via echo, e.g., or text not within <?php...?> tags). If you find yourself simply unable to organize your script in such a way to avoid any output, you can use the output buffering functions, but this may add a bit of overhead to things.
    Thanks for the reply NogDog. I had it set up so that the one echo statement was at the very bottom, but, I'm wondering if "PDF_show_xy" qualifies as an "output" type statement? Because that's the only thing I can see that's possibly creating output and that is above the "header" statements.

    I'll research the 'output buffering' functions too - I didn't know about those...

    The 3 header statements that I have at the bottom of my file, just above the one echo statement are:

    header('Content-type: application/pdf');
    header('Content-disposition: inline; filename=Test.pdf');
    header('Content-length: ' . strlen($data));

    Which work okay, until I put in the "session_start();" at the very top. Then I get headers already created type errors.

  4. #4
    Join Date
    Mar 2010
    Posts
    78
    Quote Originally Posted by redtail View Post
    Thanks for the reply NogDog. I had it set up so that the one echo statement was at the very bottom, but, I'm wondering if "PDF_show_xy" qualifies as an "output" type statement? Because that's the only thing I can see that's possibly creating output and that is above the "header" statements.

    I'll research the 'output buffering' functions too - I didn't know about those...

    The 3 header statements that I have at the bottom of my file, just above the one echo statement are:

    header('Content-type: application/pdf');
    header('Content-disposition: inline; filename=Test.pdf');
    header('Content-length: ' . strlen($data));

    Which work okay, until I put in the "session_start();" at the very top. Then I get headers already created type errors.
    I would like to know which headers the "session_start();" sets.

    I managed to get my page working this evening. I had to move things around
    and play with it a little bit. But, then I got to the point where ff was working well, but IE was giving me some nasty error msg. to the effect that it wasn't finding my page. I put in a "session_cache_limiter('public');" statement just before the "session_start();" - and voilą! it works! I'm guessing that session_start sets the cache in some way that IE didn't like...

    Anyway, it's working...

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,610
    session_start() sets a cookie header (the same as setcookie() does) to hold the session ID.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Mar 2010
    Posts
    78
    Quote Originally Posted by NogDog View Post
    session_start() sets a cookie header (the same as setcookie() does) to hold the session ID.
    Thanks, I didn't know that. Is that all that it sets?

    It seems like IE7 has some problem with this session_start in this file. I use it all the time in all my other pages, with no
    issues. So, today I've got to research the 'session_cache_limiter' and see why certain settings work, like 'public' and 'private', but the one I prefer, 'nochache' doesn't work right for me, with this page. In testing, I found that 'public' was not refreshing - ever, and I need it to refresh at each new request (in IE). FF seems to be working fine either way...

  7. #7
    Join Date
    Mar 2010
    Posts
    78
    It looks like 'session_cache_limiter ("must-revalidate");' is working much better for me. The page displays - #1 and #2, it refreshes properly displaying new content each time... Still testing, but it's looking much better... Good in FF too!

  8. #8
    Join Date
    Dec 2011
    Posts
    1
    Thank You redtail for posting this! I would have never figured this out. I just registered to post this.

  9. #9
    Join Date
    Mar 2010
    Posts
    78
    Quote Originally Posted by sammylightfoot View Post
    Thank You redtail for posting this! I would have never figured this out. I just registered to post this.
    You're welcome! Glad it helped you. I had a heck of a time with this one...And usually, if I'm having a hard time with something, other folks will be too...

    Please do the same for someone else, sometime.


    Best Regards,

    Red.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles