session problem: headers already sent

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • boss1
    New Member
    • Sep 2007
    • 45

    session problem: headers already sent

    when i run the script the following massege comes one sde of the page.

    Warning: session_start() : Cannot send session cookie - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\h tdocs\new\mnyre cpt.php:16) in C:\Program Files\Apache Group\Apache2\h tdocs\new\mnyre cpt.php on line 244


    so can any one help me to remove it.

    thanks.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    session_start() has to be used before ANYTHING is outputted to the browser, obviously something you haven't done.

    We aren't psychics so we can't tell you how to get rid of this error.
    Show your code (everything that is relevant) AND USE CODE TAGS!

    Examples:
    [php]
    echo "this will cause an error";
    session_start() ;
    [/php]
    [php]
    session_start() ;
    echo "this wont cause an error";
    [/php]

    Comment

    • TheServant
      Recognized Expert Top Contributor
      • Feb 2008
      • 1168

      #3
      Just to add something to markusn00b. Anything includes a space. So if you have
      <?php{space}
      session_start()
      ...

      Instead of:
      <?php
      session_start()
      ...

      You will get an error.

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Following is the text of an article by wildteen88, june 2006, who can explain this a lot better then I can. If you checked your code and still find no solution, show your code and we will have a look.
        Originally posted by wildteen88
        The reason why you are getting this message is because you have may have/be:
        • Whitespace before the opening php tag <?php
        • Outputting something to the browser before you use session_start, header, setcookie etc

        session_start, setcookie, header and a few other functions write header information to the web server/browser what to do. Such as when you use session_start it requests the browser to create a cookie which stores the PHPSESSID.

        If you have output before you use these functions then they are unable to send new header information as it has already been sent in the form text/html, and so you get the headers already sent error message.

        You can easily find the source of the problem by looking at the error message. As the answer is actully in there, but most people dont notice it as they may not understand what the error means. So lets take this error message as an example:

        Warning: Cannot modify header information - headers already sent by (output started at C:\server\www\t est.php:6) in C:\server\www\t est.php on line 8
        Now PHP has given us a clue here as it has told use where the output has started! Have look where it says output started at and it gives you the full path to the file and the line number. Now the line number that it stats is not usually where the output has started but where the output has ended, becuase the output could be above that line.
        So lets look at test.php:
        Code:
         1 <html>
         2 <head>
         3 <title>Header test</title>
         4 </head>
         5 <body>
         6 <?php
         7
         8 header("Location: http://www.google.com");
         9
        10 ?>
        11 </body>
        12 </html>
        As you can see line 6 is the opening PHP tag (< ?php) this isn't the output but look above that line you'll notice it has some HTML code. This html code is the cause of the error!

        So when you get this error message again look for the clue where it says output started at and goto the file and line number it says. Look above the line number and find where your output is located to.
        Hope that helps you understand why your are getting this error message. (Thanks to wildteen88)

        Ronald

        Comment

        • coolsti
          Contributor
          • Mar 2008
          • 310

          #5
          Originally posted by TheServant
          Just to add something to markusn00b. Anything includes a space. So if you have
          <?php{space}
          session_start()
          ...

          Instead of:
          <?php
          session_start()
          ...

          You will get an error.
          I am a bit confused, I do not see why the above example will lead to an error, since the space after the <?php tag will be ignored by the php interpreter and, hence, not sent out to the browser.

          I think you mean that this will give an error:
          {space}
          <?php
          session_start()
          ...

          since the space before the <?php tag will be sent out to the browser.

          Steve, Denmark

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Originally posted by coolsti
            I am a bit confused, I do not see why the above example will lead to an error, since the space after the <?php tag will be ignored by the php interpreter and, hence, not sent out to the browser.

            I think you mean that this will give an error:
            {space}
            <?php
            session_start()
            ...

            since the space before the <?php tag will be sent out to the browser.

            Steve, Denmark
            You can be absolutely sure that this will give an error! When the interpreter 'sees' the blank after the <?PHP tag, why should he ignore that? A blank is also plain data, even has a hex value! So: interpreter cannot interpret it and will send it immediately to the browser!

            Ronald

            Comment

            Working...