Cannot get a session counter to work

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tedpottel@gmail.com

    Cannot get a session counter to work

    Hi
    I cannot get sessions to work.
    I have the following code
    </body>

    <?
    session_start() ;
    $counter++;
    print "You have visited this page $counter times during this session";
    session_registe r("counter");
    ?>


    </html>



    And get the following output
    Warning: session_start() [function.sessio n-start]: Cannot send session
    cookie - headers already sent by (output started at /home/ted/
    public_html/store/test/testsession.php :15) in /home/ted/public_html/
    store/test/testsession.php on line 16

    Warning: session_start() [function.sessio n-start]: Cannot send session
    cache limiter - headers already sent (output started at /home/ted/
    public_html/store/test/testsession.php :15) in /home/ted/public_html/
    store/test/testsession.php on line 16
    You have visited this page 1 times during this session

    What is going wrong????

    -Ted
  • Michael Fesser

    #2
    Re: Cannot get a session counter to work

    ..oO(tedpottel@ gmail.com)
    >Hi
    >I cannot get sessions to work.
    >I have the following code
    ></body>
    >
    ><?
    >session_start( );
    >$counter++;
    >print "You have visited this page $counter times during this session";
    >session_regist er("counter");
    >?>
    >
    >
    ></html>
    >
    >
    >
    >And get the following output
    >Warning: session_start() [function.sessio n-start]: Cannot send session
    >cookie - headers already sent by (output started at /home/ted/
    >public_html/store/test/testsession.php :15) in /home/ted/public_html/
    >store/test/testsession.php on line 16
    >
    >Warning: session_start() [function.sessio n-start]: Cannot send session
    >cache limiter - headers already sent (output started at /home/ted/
    >public_html/store/test/testsession.php :15) in /home/ted/public_html/
    >store/test/testsession.php on line 16
    >You have visited this page 1 times during this session
    >
    >What is going wrong????
    A lot:

    * You can't have any output before the session_start() call. Move it to
    the top of the page _before_ any HTML. This should fix the "headers
    already sent" issue.

    * Use <?php instead of <?. Short open tags are unreliable.

    * Check your php.ini and make sure that error_reporting is set to E_ALL
    and display_errors are on. Your code above should throw an E_NOTICE
    error.

    * You don't need session_registe r() anymore. Use something like this to
    properly initialize and increase the counter:

    session_start() ;
    if (!isset($_SESSI ON['counter'])) {
    $_SESSION['counter'] = 1;
    } else {
    $_SESSION['counter']++;
    }

    Later simply use

    print $_SESSION['counter'];

    to output it.

    HTH
    Micha

    Comment

    • tedpottel@gmail.com

      #3
      Re: Cannot get a session counter to work

      -Thank you, it works now!!!!
      -Ted
      On Oct 5, 1:26 pm, Michael Fesser <neti...@gmx.de wrote:
      .oO(tedpot...@g mail.com)
      >
      >
      >
      >
      >
      Hi
      I cannot get sessions to work.
      I have the following code
      </body>
      >
      <?
      session_start() ;
      $counter++;
      print "You have visited this page $counter times during this session";
      session_registe r("counter");
      ?>
      >
      </html>
      >
      And get the following output
      Warning: session_start() [function.sessio n-start]: Cannot send session
      cookie - headers already sent by (output started at /home/ted/
      public_html/store/test/testsession.php :15) in /home/ted/public_html/
      store/test/testsession.php on line 16
      >
      Warning: session_start() [function.sessio n-start]: Cannot send session
      cache limiter - headers already sent (output started at /home/ted/
      public_html/store/test/testsession.php :15) in /home/ted/public_html/
      store/test/testsession.php on line 16
      You have visited this page 1 times during this session
      >
      What is going wrong????
      >
      A lot:
      >
      * You can't have any output before the session_start() call. Move it to
      the top of the page _before_ any HTML. This should fix the "headers
      already sent" issue.
      >
      * Use <?php instead of <?. Short open tags are unreliable.
      >
      * Check your php.ini and make sure that error_reporting is set to E_ALL
      and display_errors are on. Your code above should throw an E_NOTICE
      error.
      >
      * You don't need session_registe r() anymore. Use something like this to
      properly initialize and increase the counter:
      >
      session_start() ;
      if (!isset($_SESSI ON['counter'])) {
        $_SESSION['counter'] = 1;} else {
      >
        $_SESSION['counter']++;
      >
      }
      >
      Later simply use
      >
      print $_SESSION['counter'];
      >
      to output it.
      >
      HTH
      Micha- Hide quoted text -
      >
      - Show quoted text -

      Comment

      • Michael Fesser

        #4
        Re: Cannot get a session counter to work

        ..oO(tedpottel@ gmail.com)
        >-Thank you, it works now!!!!
        You're welcome.

        Micha

        Comment

        Working...