SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Session or Doctype first?

    I was having trouble with errors trying to put Session_start() near the top of my web page - until I put it above the Doctype stuff. But I thought Doctype had to be the first thing on an html page. (Everybody wants to be first).
    So now I have Session_start() on line 1 surrounded by php tags, and Doctype on line 2.
    Will the Doctype information be processed?

    Second question: I didn't have any problem in my shop creating this on Windows. I can't even make it happen. The problem only occurs at my ISP on Linux. It seems like the Doctype is sending header info or something that disagrees with Session_start, which is trying to set a cookie. Why would it be so different?

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The session_start() is a server-side scripting statement that will result in a Cookie header being sent in the HTTP response. All headers must precede the response body (which starts with the doctype declaration).

    Unless you use output buffering of some sort, you need to start the session before you output anything (including the doctype declaration) to the response stream.
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But, does anyone know if:
    1 Will the Doctype be processed properly
    2. Why does it not have the problem in my shop.

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,061
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by WebPat View Post
    But, does anyone know if:
    1 Will the Doctype be processed properly
    Yes it will be sent properly, since session_start() produces no real output (i.e., will not be visible in the HTML). If your still confused please open the source of your web page and look at the first line. Voila, the doctype, nothing to be seen about session_start().

    Quote Originally Posted by WebPat View Post
    2. Why does it not have the problem in my shop.
    As AutisticCuckoo already suggested, your shop probably has output buffering enabled in the php.ini
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, that's it. Output buffering is set up in my php.ini.
    I didn't read the earlier post that way. I was only thinking about me setting it up in my code if I wanted to. That didn't make me think that maybe it was already set up that way.

    And I have been viewing source code all day. I just wasn't sure about why it had to be at the top, or if seeing it there in the running code was good enough. Not sure how to tell if it's working either - whatever it does :>)

    This session project is to log in users and display their name at the top of the pages - pretty typical. But now I guess I have to go through every page on the site and start a session as the first thing. Unless anybody has any better ideas?


Tags for this Thread

Bookmarks

Posting Permissions

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