11 Replies Latest reply on May 7, 2006 4:51 PM by Newsgroup_User

    - Putting an include *before* the doctype :-/

    Level 7
      Someone wrote a PHP comments script for my site, and we finally got it
      working, but it involved adding the following include on the 1st line of
      every page on the site :

      <? php require_once('functions.php'); ?>

      Normally, this sort of thing wouldn't bother me, except... we're talking
      about the 1st line of the page. Before the body tag, before the head tag...
      before the darn doctype, man! :-)

      Now is it me, or is that a highly unusual way of doing things?

      The very same line, placed right after the opening body tag, produces this
      error :

      Warning: session_start(): Cannot send session cookie - headers already sent
      by (output started at
      /home/.maks/username/yourdomain.com/folder/filewiththeinclude.php:10) in
      /home/.maks/username/yourdomain.com/comments/db.inc.php on line 49

      I can't help but feel that I was asked to put the include at the 1st line of
      the page simply to mask this error msg.

      Not being a fan of anything generating errors -- even if it's technically
      working -- I thought to seek advice here. Is this a sign of shoddy
      programming? Am I going to regret going ahead with this later on? Or am I
      making too much of this, and I should simply shut up and put the include on
      line 1 as I was told to do without imagining problems where none exist?

      Thanks.


        • 1. Re: - Putting an include *before* the doctype :-/
          Level 7
          .oO(Reese)

          >The very same line, placed right after the opening body tag, produces this
          >error :
          >
          >Warning: session_start(): Cannot send session cookie - headers already sent
          >by (output started at
          >/home/.maks/username/yourdomain.com/folder/filewiththeinclude.php:10) in
          >/home/.maks/username/yourdomain.com/comments/db.inc.php on line 49
          >
          >I can't help but feel that I was asked to put the include at the 1st line of
          >the page simply to mask this error msg.

          Read the error message more carefully. It doesn't complain about your
          functions.php script, but about one of the included files.

          >Not being a fan of anything generating errors -- even if it's technically
          >working --

          Currently it won't work as expected. You definitely have to fix it.

          Micha
          • 2. Re: - Putting an include *before* the doctype :-/
            Level 7
            "Michael Fesser" <netizen@gmx.de> wrote in message
            news:efvq52dp4059ig41drfa0csi16d90hh2al@4ax.com...
            > .oO(Reese)
            >
            >>The very same line, placed right after the opening body tag, produces this
            >>error :
            >>
            >>Warning: session_start(): Cannot send session cookie - headers already
            >>sent
            >>by (output started at
            >>/home/.maks/username/yourdomain.com/folder/filewiththeinclude.php:10) in
            >>/home/.maks/username/yourdomain.com/comments/db.inc.php on line 49
            >>
            >>I can't help but feel that I was asked to put the include at the 1st line
            >>of
            >>the page simply to mask this error msg.
            >
            > Read the error message more carefully. It doesn't complain about your
            > functions.php script, but about one of the included files.
            >
            >>Not being a fan of anything generating errors -- even if it's technically
            >>working --
            >
            > Currently it won't work as expected. You definitely have to fix it.

            When I brought this to the programmer's attention, he sent me this back :

            To hide the errors:
            http://ca.php.net/error_reporting

            What causes the "problem":
            http://ca.php.net/manual/en/function.session-start.php
            If you read that, you will see if anything is sent out before session start,
            then it will not work.

            Now not sure why it works on mine and not yours (other then the server
            config) but just add that extra line and everything is ok.

            :-S


            • 3. Re: - Putting an include *before* the doctype :-/
              Level 7
              .oO(Reese)

              >When I brought this to the programmer's attention, he sent me this back :
              >
              >To hide the errors:
              > http://ca.php.net/error_reporting

              Change the programmer.

              On a development machine error_reporting _must_ be set to E_ALL. Errors
              have to be fixed, not hidden.

              >What causes the "problem":
              > http://ca.php.net/manual/en/function.session-start.php
              >If you read that, you will see if anything is sent out before session start,
              >then it will not work.
              >
              >Now not sure why it works on mine and not yours (other then the server
              >config) but just add that extra line and everything is ok.

              It's clearly a bug in the script, which will cause problems. There are
              two ways to solve it:

              1) Check the script and fix it.
              2) Use output buffering. Maybe he uses this on his machine, which would
              prevent the error from happening.

              Micha
              • 4. Re: - Putting an include *before* the doctype :-/
                Level 7
                Reese wrote:
                > <? php require_once('functions.php'); ?>
                >
                > Normally, this sort of thing wouldn't bother me, except... we're talking
                > about the 1st line of the page. Before the body tag, before the head tag...
                > before the darn doctype, man! :-)
                >
                > Now is it me, or is that a highly unusual way of doing things?

                It's perfectly normal. If you look at the PHP code auto-generated by
                Dreamweaver, most of it goes before the DOCTYPE declaration. As long as
                the PHP code doesn't generate any output to the browser, everything will
                be fine.

                > The very same line, placed right after the opening body tag, produces this
                > error :
                >
                > Warning: session_start(): Cannot send session cookie - headers already sent
                > by (output started at
                > /home/.maks/username/yourdomain.com/folder/filewiththeinclude.php:10) in
                > /home/.maks/username/yourdomain.com/comments/db.inc.php on line 49
                >
                > I can't help but feel that I was asked to put the include at the 1st line of
                > the page simply to mask this error msg.

                You've misunderstood the error message. What has happened is that either
                the include file, or the way you have included it, has generated output
                to the browser. The most common reason for this happening is any
                whitespace or new lines before the opening PHP tag. Another cause is
                whitespace or new lines either before OR AFTER the PHP tags in the
                include file.

                Whitespace and new lines outside PHP tags are interpreted by the web
                server as output. Any output, even if if contains nothing visible, will
                trigger this error. Clean up the whitespace, and the problem should
                disappear.

                --
                David Powers
                Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
                Author, "Foundation PHP 5 for Flash" (friends of ED)
                http://foundationphp.com/
                • 5. Re: - Putting an include *before* the doctype :-/
                  Level 7
                  On Sun, 07 May 2006 10:40:23 +0100, David Powers <david@example.com>
                  wrote:

                  >> I can't help but feel that I was asked to put the include at the 1st line of
                  >> the page simply to mask this error msg.
                  >
                  >You've misunderstood the error message.


                  Perhaps I've misunderstood as well. It would appear to me that the
                  included file includes a session_start. Moving the line that includes
                  that file down to "right after the opening body tag" would certainly
                  generate the described error. The include directive would have to be the
                  first line in the page.

                  Gary
                  • 6. Re: - Putting an include *before* the doctype :-/
                    Level 7
                    Gary White wrote:
                    > Perhaps I've misunderstood as well. It would appear to me that the
                    > included file includes a session_start.

                    You may be right, Gary. I can understand people not wanting to reveal
                    their server file structure in too much detail, but attempting to
                    decipher error messages with obscured filenames and no clue as to what
                    the files actually contain doesn't make like easy for others trying to help.

                    --
                    David Powers
                    Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
                    Author, "Foundation PHP 5 for Flash" (friends of ED)
                    http://foundationphp.com/
                    • 7. Re: - Putting an include *before* the doctype :-/
                      Level 7
                      On Sun, 7 May 2006 12:32:37 -0400, "Reese" <no@spam.com> wrote:

                      >Allow me to share more, then. :-)


                      The problem you're having is just what I expected. The file db.inc.php
                      includes a session_start. The include for that file MUST be before ANY
                      output goes to the browser. Your original concern about this was putting
                      something before the doctype. The concern is not an issue. The included
                      files don't output anything to the browser. They only include a bit of
                      server processing and some functions that are used later. If processed
                      by the server, the first line the browser sees will still be the
                      doctype. Put the include call back to the very first line in the file
                      and your error message will go away. View the processed output in a
                      browser and you'll see that the doctype is still the first line.


                      Gary
                      • 8. Re: - Putting an include *before* the doctype :-/
                        Level 7
                        "Gary White" <reply@newsgroup.please> wrote in message
                        news:7r9s521ui8m8e50bv0u32dtlstbtttduhf@4ax.com...
                        > On Sun, 7 May 2006 12:32:37 -0400, "Reese" <no@spam.com> wrote:
                        >
                        >>Allow me to share more, then. :-)
                        >
                        > The problem you're having is just what I expected. The file db.inc.php
                        > includes a session_start. The include for that file MUST be before ANY
                        > output goes to the browser. Your original concern about this was putting
                        > something before the doctype. The concern is not an issue. The included
                        > files don't output anything to the browser. They only include a bit of
                        > server processing and some functions that are used later. If processed
                        > by the server, the first line the browser sees will still be the
                        > doctype. Put the include call back to the very first line in the file
                        > and your error message will go away. View the processed output in a
                        > browser and you'll see that the doctype is still the first line.

                        The strange thing about it is that on the programmer's own server, using the
                        very same files (literally downloading the page I put the includes in and
                        all its dependent files, as well as the unedited /talkback folder where all
                        the functions are stored) -- he doesn't need that extra line (before the
                        doctype, which calls functions.php) to make the script work.

                        On my server, it would generate those cookie errors until I put that extra
                        line in, at his request. But why do I need it and not him? He couldn't tell
                        me.

                        That's why I'm trying to get this to work without it. I guess unnecessary
                        code just worries me. :-) "Who knows why it works, but it does, go with
                        that" isn't very re-assuring coming from the programmer.


                        • 9. Re: - Putting an include *before* the doctype :-/
                          Level 7
                          Reese wrote:
                          > The strange thing about it is that on the programmer's own server, using the
                          > very same files (literally downloading the page I put the includes in and
                          > all its dependent files, as well as the unedited /talkback folder where all
                          > the functions are stored) -- he doesn't need that extra line (before the
                          > doctype, which calls functions.php) to make the script work.

                          If he's not getting any errors, it means that his server has
                          output_buffering turned on.

                          > That's why I'm trying to get this to work without it. I guess unnecessary
                          > code just worries me. :-) "Who knows why it works, but it does, go with
                          > that" isn't very re-assuring coming from the programmer.

                          Not very reassuring at all, particularly since it's a relatively
                          fundamental piece of knowledge that a "programmer" should have. Some
                          shared hosts turn on output_buffering even though it's less efficient.
                          It saves them the hassle of inexperienced people complaining about
                          "headers already sent". Usually, the problem is very easily solved once
                          you understand why it happens.

                          --
                          David Powers
                          Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
                          Author, "Foundation PHP 5 for Flash" (friends of ED)
                          http://foundationphp.com/
                          • 10. Re: - Putting an include *before* the doctype :-/
                            Level 7
                            "David Powers" <david@example.com> wrote in message
                            news:e3laak$gmj$1@forums.macromedia.com...
                            > Reese wrote:
                            >> The strange thing about it is that on the programmer's own server, using
                            >> the very same files (literally downloading the page I put the includes in
                            >> and all its dependent files, as well as the unedited /talkback folder
                            >> where all the functions are stored) -- he doesn't need that extra line
                            >> (before the doctype, which calls functions.php) to make the script work.
                            >
                            > If he's not getting any errors, it means that his server has
                            > output_buffering turned on.
                            >
                            >> That's why I'm trying to get this to work without it. I guess unnecessary
                            >> code just worries me. :-) "Who knows why it works, but it does, go with
                            >> that" isn't very re-assuring coming from the programmer.
                            >
                            > Not very reassuring at all, particularly since it's a relatively
                            > fundamental piece of knowledge that a "programmer" should have.

                            Well, you get what you pay for, and he only charged me $50 for the whole
                            thing (all I could afford for this feature), so... I can't complain TOO
                            much.

                            > Some shared hosts turn on output_buffering even though it's less
                            > efficient. It saves them the hassle of inexperienced people complaining
                            > about "headers already sent". Usually, the problem is very easily solved
                            > once you understand why it happens.

                            Then you too, like Gary, recommend I put the "function.php" include before
                            the doctype on every page, rather than simply turn output_buffering on the
                            way the programmer likely has it set up on his server?

                            At high volumes of traffic, which of the two solutions (the added include on
                            top, or buffering on) is the most efficient?

                            Thanks!


                            • 11. Re: - Putting an include *before* the doctype :-/
                              Level 7
                              Reese wrote:
                              > At high volumes of traffic, which of the two solutions (the added include on
                              > top, or buffering on) is the most efficient?

                              Buffering is always less efficient.

                              --
                              David Powers
                              Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
                              Author, "Foundation PHP 5 for Flash" (friends of ED)
                              http://foundationphp.com/