Yet Another "session_start() Cannot send session cache limiter - headers already sent" Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • three-eight-hotel

    #1

    Yet Another "session_start() Cannot send session cache limiter - headers already sent" Question

    I'm somewhat of a newbie to PHP coding, but have developed a site
    using the technology, and have been pleasantly surprised by the
    capabilities offered. I am more comfortable in the ASP world, however
    and am really struggling with managing sessions in PHP, based on my
    experiences with managing sessions in ASP.

    99.9% of the feedback I have seen when dealing with the errors has
    referred to having whitespace before the <?php or after the ?>. I
    have opened my source in several different editors and can't find that
    to be the case with my code. Aside from having general issues with
    the basic methodology in which I am managing sessions, I am completely
    perplexed as to why I am getting these errors, from time to time...

    For those who might have recommendations on an overall session
    management strategy, I basically want to allow my user to come into my
    site, through any page that might be bookmarked. Not all pages are
    going to require a session, but they may be required to have a session
    value set in order to perform certain administrative functions (which
    requires a successful login), so I would need to check for the
    existence of a session value and log them in and set it, if it doesn't
    exist...

    Now to my specific problem...

    I have an include page called master_session. php, which looks like
    this (no whitespaces before or after php tags):
    =============== =============== ===
    <?php
    if (! isset($ADMIN_se ssion)) {
    ob_start();
    ob_clean();
    session_start() ;
    session_registe r("ADMIN_sessio n");
    }
    ?>
    =============== =============== ===

    I have a login page, which looks like this (no whitespace before the
    include of master_session)
    =============== =============== ===
    <?php require_once($_ SERVER['DOCUMENT_ROOT'].'/common/includes/
    master_session. php'); ?>
    <?php if ($content_ident ifier == false){
    $content_identi fier = 'parish';}?>
    <? $page_title = "Administration "; ?>
    <?php require_once($_ SERVER['DOCUMENT_ROOT'].'/common/includes/
    master_header.p hp'); ?>
    <?php require_once($_ SERVER['DOCUMENT_ROOT'].'/common/includes/
    master_nav.php' ); ?>
    <?php require_once($_ SERVER['DOCUMENT_ROOT'].'/common/includes/
    bg100.php'); ?>
    <!----CONTENT AREA -- PLACE INCLUDE PATH AND FILE NAME BELOW---->
    <form name="criteria_ form" action="adminva lidation.php" method="post">
    <table height="100%" border="0" width="81%" align="center"
    background="/common/bkgrnds/bg100.gif"">
    <tr>
    <td colspan="2" height="40">&nb sp;</td>
    </tr>
    <? if (${"login"} == "invalid")
    echo "The username/password you have entered is invalid, or you do
    not have privileges to perform administrative functions. If you feel
    you've received this message in error, please contact your system
    administrator." ;
    ?>
    <tr>
    <td colspan="2" align="center"> <strong>Pleas e Login</strong></
    td>
    </tr>
    <tr>
    <td colspan="2" height="40">&nb sp;</td>
    </tr>
    <tr>
    <td align="right">U sername&nbsp;</td>
    <td><input type="text" name="user_nm" size="8" maxlength="8">
    </td>
    </tr>
    <tr>
    <td align="right">P assword&nbsp;</td>
    <td><input type="password" name="password" size="10"
    maxlength="10">
    </td>
    </tr>
    <tr>
    <td colspan="2" height="40">&nb sp;</td>
    </tr>
    <tr>
    <td colspan="2" align="center"> <input type="submit"
    name="Submit" value="Submit">
    </td>
    </tr>
    <tr>
    <td><input type="hidden" name="content_i dentifier" value="<?echo
    ${content_ident ifier}?>"></td>
    </tr>
    </table>
    </form>
    <!----END CONTENT AREA --- NO CHANGES BELOW THIS LINE-------->
    <?php require_once($_ SERVER['DOCUMENT_ROOT'].'/common/includes/
    master_footer.p hp'); ?>
    =============== =============== =============== =

    master_header.p hp is where my HTML code starts:
    =============== =========
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>

    etc............ .......
    =============== =========


    Is there anything in here that jumps out at anyone, as to why I'm
    getting the error? The exact error is:

    Warning: session_start() [function.sessio n-start]: Cannot send session
    cache limiter - headers already sent (output started at /home/holytrin/
    public_html/common/admin/admin.php:2) in /home/holytrin/public_html/
    common/includes/master_session. php on line 4

    btw.. I do an ob_end_flush() in the master_footer.p hp file, on the
    very last line...

    Any help would be greatly appreciated!!!

    Best Regards,
    Todd

  • Colin McKinnon

    #2
    Re: Yet Another &quot;session_s tart() Cannot send session cache limiter - headers already sent&quot; Question

    three-eight-hotel wrote:
    I'm somewhat of a newbie to PHP coding,
    <snip>
    99.9% of the feedback I have seen when dealing with the errors has
    referred to having whitespace before the <?php or after the ?>.
    <snip>
    <?php
    <snip>
    <?php require_once($_ SERVER['DOCUMENT_ROOT'].'/common/includes/
    <snip>

    Hi Todd,

    You're more likely to get sensible answers to your questions if you try to
    keep to the point - read this:



    From what you've posted the most likely causes of the problem are:

    1) the BOM marker at the start of a UTF-8 encoded source file - you *must*
    save your files as ASCII.

    2) an auto-prepend file specified in the php.ini or webserver config.

    HTH

    C.

    Comment

    • shimmyshack

      #3
      Re: Yet Another &quot;session_s tart() Cannot send session cache limiter - headers already sent&quot; Question

      On Mar 30, 9:27 pm, "three-eight-hotel" <t...@thepeters onranch.com>
      wrote:
      I'm somewhat of a newbie to PHP coding, but have developed a site
      using the technology, and have been pleasantly surprised by the
      capabilities offered. I am more comfortable in the ASP world, however
      and am really struggling with managing sessions in PHP, based on my
      experiences with managing sessions in ASP.
      >
      99.9% of the feedback I have seen when dealing with the errors has
      referred to having whitespace before the <?php or after the ?>. I
      have opened my source in several different editors and can't find that
      to be the case with my code. Aside from having general issues with
      the basic methodology in which I am managing sessions, I am completely
      perplexed as to why I am getting these errors, from time to time...
      >
      For those who might have recommendations on an overall session
      management strategy, I basically want to allow my user to come into my
      site, through any page that might be bookmarked. Not all pages are
      going to require a session, but they may be required to have a session
      value set in order to perform certain administrative functions (which
      requires a successful login), so I would need to check for the
      existence of a session value and log them in and set it, if it doesn't
      exist...
      >
      Now to my specific problem...
      >
      I have an include page called master_session. php, which looks like
      this (no whitespaces before or after php tags):
      =============== =============== ===
      <?php
      if (! isset($ADMIN_se ssion)) {
      ob_start();
      ob_clean();
      session_start() ;
      session_registe r("ADMIN_sessio n");
      }
      ?>
      =============== =============== ===
      >
      I have a login page, which looks like this (no whitespace before the
      include of master_session)
      =============== =============== ===
      <?php require_once($_ SERVER['DOCUMENT_ROOT'].'/common/includes/
      master_session. php'); ?>
      <?php if ($content_ident ifier == false){
      $content_identi fier = 'parish';}?>
      <? $page_title = "Administration "; ?>
      <?php require_once($_ SERVER['DOCUMENT_ROOT'].'/common/includes/
      master_header.p hp'); ?>
      <?php require_once($_ SERVER['DOCUMENT_ROOT'].'/common/includes/
      master_nav.php' ); ?>
      <?php require_once($_ SERVER['DOCUMENT_ROOT'].'/common/includes/
      bg100.php'); ?>
      <!----CONTENT AREA -- PLACE INCLUDE PATH AND FILE NAME BELOW---->
      <form name="criteria_ form" action="adminva lidation.php" method="post">
      <table height="100%" border="0" width="81%" align="center"
      background="/common/bkgrnds/bg100.gif"">
      <tr>
      <td colspan="2" height="40">&nb sp;</td>
      </tr>
      <? if (${"login"} == "invalid")
      echo "The username/password you have entered is invalid, or you do
      not have privileges to perform administrative functions. If you feel
      you've received this message in error, please contact your system
      administrator." ;
      ?>
      <tr>
      <td colspan="2" align="center"> <strong>Pleas e Login</strong></
      td>
      </tr>
      <tr>
      <td colspan="2" height="40">&nb sp;</td>
      </tr>
      <tr>
      <td align="right">U sername&nbsp;</td>
      <td><input type="text" name="user_nm" size="8" maxlength="8">
      </td>
      </tr>
      <tr>
      <td align="right">P assword&nbsp;</td>
      <td><input type="password" name="password" size="10"
      maxlength="10">
      </td>
      </tr>
      <tr>
      <td colspan="2" height="40">&nb sp;</td>
      </tr>
      <tr>
      <td colspan="2" align="center"> <input type="submit"
      name="Submit" value="Submit">
      </td>
      </tr>
      <tr>
      <td><input type="hidden" name="content_i dentifier" value="<?echo
      ${content_ident ifier}?>"></td>
      </tr>
      </table>
      </form>
      <!----END CONTENT AREA --- NO CHANGES BELOW THIS LINE-------->
      <?php require_once($_ SERVER['DOCUMENT_ROOT'].'/common/includes/
      master_footer.p hp'); ?>
      =============== =============== =============== =
      >
      master_header.p hp is where my HTML code starts:
      =============== =========
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
      <head>
      >
      etc............ .......
      =============== =========
      >
      Is there anything in here that jumps out at anyone, as to why I'm
      getting the error? The exact error is:
      >
      Warning: session_start() [function.sessio n-start]: Cannot send session
      cache limiter - headers already sent (output started at /home/holytrin/
      public_html/common/admin/admin.php:2) in /home/holytrin/public_html/
      common/includes/master_session. php on line 4
      >
      btw.. I do an ob_end_flush() in the master_footer.p hp file, on the
      very last line...
      >
      Any help would be greatly appreciated!!!
      >
      Best Regards,
      Todd
      the essential problem is one of sending a header, response headers
      have to be sent before any response body, so whenever you call any
      function before session_start() which sends a header, you might have
      trouble if the function returns output.
      calling session_start first will fix this of course, and it doesnt
      affect the use of ob_start() since that is about buffering response
      body output.

      what you are doing is saying when you call ob_start is "everything
      after this point should be captured and stored in the buffer", but
      what you then output is a header, something which can be sent first,
      and should not be captured.

      Comment

      • shimmyshack

        #4
        Re: Yet Another &quot;session_s tart() Cannot send session cache limiter - headers already sent&quot; Question

        On Mar 30, 9:27 pm, "three-eight-hotel" <t...@thepeters onranch.com>
        wrote:
        I'm somewhat of a newbie to PHP coding, but have developed a site
        using the technology, and have been pleasantly surprised by the
        capabilities offered. I am more comfortable in the ASP world, however
        and am really struggling with managing sessions in PHP, based on my
        experiences with managing sessions in ASP.
        >
        99.9% of the feedback I have seen when dealing with the errors has
        referred to having whitespace before the <?php or after the ?>. I
        have opened my source in several different editors and can't find that
        to be the case with my code. Aside from having general issues with
        the basic methodology in which I am managing sessions, I am completely
        perplexed as to why I am getting these errors, from time to time...
        >
        For those who might have recommendations on an overall session
        management strategy, I basically want to allow my user to come into my
        site, through any page that might be bookmarked. Not all pages are
        going to require a session, but they may be required to have a session
        value set in order to perform certain administrative functions (which
        requires a successful login), so I would need to check for the
        existence of a session value and log them in and set it, if it doesn't
        exist...
        >
        Now to my specific problem...
        >
        I have an include page called master_session. php, which looks like
        this (no whitespaces before or after php tags):
        =============== =============== ===
        <?php
        if (! isset($ADMIN_se ssion)) {
        ob_start();
        ob_clean();
        session_start() ;
        session_registe r("ADMIN_sessio n");
        }
        ?>
        =============== =============== ===
        >
        I have a login page, which looks like this (no whitespace before the
        include of master_session)
        =============== =============== ===
        <?php require_once($_ SERVER['DOCUMENT_ROOT'].'/common/includes/
        master_session. php'); ?>
        <?php if ($content_ident ifier == false){
        $content_identi fier = 'parish';}?>
        <? $page_title = "Administration "; ?>
        <?php require_once($_ SERVER['DOCUMENT_ROOT'].'/common/includes/
        master_header.p hp'); ?>
        <?php require_once($_ SERVER['DOCUMENT_ROOT'].'/common/includes/
        master_nav.php' ); ?>
        <?php require_once($_ SERVER['DOCUMENT_ROOT'].'/common/includes/
        bg100.php'); ?>
        <!----CONTENT AREA -- PLACE INCLUDE PATH AND FILE NAME BELOW---->
        <form name="criteria_ form" action="adminva lidation.php" method="post">
        <table height="100%" border="0" width="81%" align="center"
        background="/common/bkgrnds/bg100.gif"">
        <tr>
        <td colspan="2" height="40">&nb sp;</td>
        </tr>
        <? if (${"login"} == "invalid")
        echo "The username/password you have entered is invalid, or you do
        not have privileges to perform administrative functions. If you feel
        you've received this message in error, please contact your system
        administrator." ;
        ?>
        <tr>
        <td colspan="2" align="center"> <strong>Pleas e Login</strong></
        td>
        </tr>
        <tr>
        <td colspan="2" height="40">&nb sp;</td>
        </tr>
        <tr>
        <td align="right">U sername&nbsp;</td>
        <td><input type="text" name="user_nm" size="8" maxlength="8">
        </td>
        </tr>
        <tr>
        <td align="right">P assword&nbsp;</td>
        <td><input type="password" name="password" size="10"
        maxlength="10">
        </td>
        </tr>
        <tr>
        <td colspan="2" height="40">&nb sp;</td>
        </tr>
        <tr>
        <td colspan="2" align="center"> <input type="submit"
        name="Submit" value="Submit">
        </td>
        </tr>
        <tr>
        <td><input type="hidden" name="content_i dentifier" value="<?echo
        ${content_ident ifier}?>"></td>
        </tr>
        </table>
        </form>
        <!----END CONTENT AREA --- NO CHANGES BELOW THIS LINE-------->
        <?php require_once($_ SERVER['DOCUMENT_ROOT'].'/common/includes/
        master_footer.p hp'); ?>
        =============== =============== =============== =
        >
        master_header.p hp is where my HTML code starts:
        =============== =========
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
        <html>
        <head>
        >
        etc............ .......
        =============== =========
        >
        Is there anything in here that jumps out at anyone, as to why I'm
        getting the error? The exact error is:
        >
        Warning: session_start() [function.sessio n-start]: Cannot send session
        cache limiter - headers already sent (output started at /home/holytrin/
        public_html/common/admin/admin.php:2) in /home/holytrin/public_html/
        common/includes/master_session. php on line 4
        >
        btw.. I do an ob_end_flush() in the master_footer.p hp file, on the
        very last line...
        >
        Any help would be greatly appreciated!!!
        >
        Best Regards,
        Todd
        btw, am I to assume you are using ob_start being you are implementing
        some kind of compression - if not, then don't bother using it.
        Also convert all short tags <? to <?php which is the standard these
        days, and will make your scripts easier to port onwards, and when you
        finally turn off short tags in the php.ini (or upgrade) you will be
        able to use <?xml style declarations without conflicts.
        Also I assume you understand about turning globals off, it's just that
        if you don't asking questions like
        if( $content_identi fier == false )
        can get you into trouble because later you print that value out (which
        you set to parish but could be overridden to something nasty) in the
        markup un-filtered.
        I should say that I am not in full possession of the facts so it isn't
        intended as a -ve critique, more an observation based on what I can
        see.

        Comment

        • shawn.fowler@gmail.com

          #5
          Re: Yet Another &quot;session_s tart() Cannot send session cache limiter - headers already sent&quot; Question

          On Mar 30, 4:27 pm, "three-eight-hotel" <t...@thepeters onranch.com>
          wrote:
          I'm somewhat of a newbie to PHP coding, but have developed a site
          using the technology, and have been pleasantly surprised by the
          capabilities offered. I am more comfortable in the ASP world, however
          and am really struggling with managing sessions in PHP, based on my
          experiences with managing sessions in ASP.
          >
          99.9% of the feedback I have seen when dealing with the errors has
          referred to having whitespace before the <?php or after the ?>. I
          have opened my source in several different editors and can't find that
          to be the case with my code. Aside from having general issues with
          the basic methodology in which I am managing sessions, I am completely
          perplexed as to why I am getting these errors, from time to time...
          >
          For those who might have recommendations on an overallsession
          management strategy, I basically want to allow my user to come into my
          site, through any page that might be bookmarked. Not all pages are
          going to require asession, but they may be required to have asession
          value set in order to perform certain administrative functions (which
          requires a successful login), so I would need to check for the
          existence of asessionvalue and log them in and set it, if it doesn't
          exist...
          >
          Now to my specific problem...
          >
          I have an include page called master_session. php, which looks like
          this (no whitespaces before or after php tags):
          =============== =============== ===
          <?php
          if (! isset($ADMIN_se ssion)) {
          ob_start();
          ob_clean();
          session_start() ;
          session_registe r("ADMIN_sessio n");
          }
          ?>
          =============== =============== ===
          >
          I have a login page, which looks like this (no whitespace before the
          include of master_session)
          =============== =============== ===
          <?php require_once($_ SERVER['DOCUMENT_ROOT'].'/common/includes/
          master_session. php'); ?>
          <?php if ($content_ident ifier == false){
          $content_identi fier = 'parish';}?>
          <? $page_title = "Administration "; ?>
          <?php require_once($_ SERVER['DOCUMENT_ROOT'].'/common/includes/
          master_header.p hp'); ?>
          <?php require_once($_ SERVER['DOCUMENT_ROOT'].'/common/includes/
          master_nav.php' ); ?>
          <?php require_once($_ SERVER['DOCUMENT_ROOT'].'/common/includes/
          bg100.php'); ?>
          <!----CONTENT AREA -- PLACE INCLUDE PATH AND FILE NAME BELOW---->
          <form name="criteria_ form" action="adminva lidation.php" method="post">
          <table height="100%" border="0" width="81%" align="center"
          background="/common/bkgrnds/bg100.gif"">
          <tr>
          <td colspan="2" height="40">&nb sp;</td>
          </tr>
          <? if (${"login"} == "invalid")
          echo "The username/password you have entered is invalid, or you do
          not have privileges to perform administrative functions. If you feel
          you've received this message in error, please contact your system
          administrator." ;
          ?>
          <tr>
          <td colspan="2" align="center"> <strong>Pleas e Login</strong></
          td>
          </tr>
          <tr>
          <td colspan="2" height="40">&nb sp;</td>
          </tr>
          <tr>
          <td align="right">U sername&nbsp;</td>
          <td><input type="text" name="user_nm" size="8" maxlength="8">
          </td>
          </tr>
          <tr>
          <td align="right">P assword&nbsp;</td>
          <td><input type="password" name="password" size="10"
          maxlength="10">
          </td>
          </tr>
          <tr>
          <td colspan="2" height="40">&nb sp;</td>
          </tr>
          <tr>
          <td colspan="2" align="center"> <input type="submit"
          name="Submit" value="Submit">
          </td>
          </tr>
          <tr>
          <td><input type="hidden" name="content_i dentifier" value="<?echo
          ${content_ident ifier}?>"></td>
          </tr>
          </table>
          </form>
          <!----END CONTENT AREA --- NO CHANGES BELOW THIS LINE-------->
          <?php require_once($_ SERVER['DOCUMENT_ROOT'].'/common/includes/
          master_footer.p hp'); ?>
          =============== =============== =============== =
          >
          master_header.p hp is where my HTML code starts:
          =============== =========
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
          <head>
          >
          etc............ .......
          =============== =========
          >
          Is there anything in here that jumps out at anyone, as to why I'm
          getting the error? The exact error is:
          >
          Warning: session_start() [function.sessio n-start]:Cannotsendsess ioncachelimiter- headers already sent (output started at /home/holytrin/
          public_html/common/admin/admin.php:2) in /home/holytrin/public_html/
          common/includes/master_session. php on line 4
          >
          btw.. I do an ob_end_flush() in the master_footer.p hp file, on the
          very last line...
          >
          Any help would be greatly appreciated!!!
          >
          Best Regards,
          Todd
          Hi. I've been having the same problem and I just figured it out. You
          can't have anything come before the session call (session_start( )).
          You can't even have a blank line before the "<?php" tag. That's what
          happened to me. The session call worked fine on all but one of my
          pages. I put session_start() at the very top of my header.php file (<?
          php session_start() ; ?>), which is on the very first line of each of
          my pages (<?php include ('includes/header.php');). However, on one of
          my pages, the code looked like this (I added the line numbers to
          emphasize the point):

          1.
          2. <?php include ('includes/header.php');
          3. $page_title = 'Title';

          I know it sounds stupid, but the problem was that empty line of code
          before my <?php tag. I deleted the first line so the code looked like
          this:

          1. <?php include ('includes/header.php');
          2. $page_title = 'Title';

          And it works fine.

          Comment

          Working...