SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Struggling with Sessions

    Hi, I'm trying to use sessions for the first time and I'm getting the following message:

    Warning: session_start(): Cannot send session cache limiter - headers already sent

    My session_start() command is at the top of the code so it is before any headers. However, the page is reloading when a form is submitted - could that be the problem?

    I'd be really grateful for any suggestions.

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,804
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    yes, the reload will cause you a problem.
    at the top of your page above session_start(), put ob_start();
    to send the output to the buffer.

    That should sort you out.

    Spike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Evangelist ClickHeRe's Avatar
    Join Date
    Mar 2005
    Location
    Ottawa, Canada
    Posts
    580
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you cannot call the session function after you have already sent HTML code to the client whether intentionally (echo/print) or not (leftover space in code between the <?php ?> tags)

    Now, you can also use output buffering with ob_start( ) to capture anything sent to the browser before you are ready to do so. It keeps the data in memory cache until you call ob_get_clean( ) to retrieve the contents and finally send it to the client.

    PHP Code:
    <?php

    ob_start
    ( );

    ....

    session_start( );

    ...

    $contents ob_get_clean( );
    echo 
    $contents;

    ?>
    David

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My session_start() command is at the top of the code so it is before any headers. However, the page is reloading when a form is submitted - could that be the problem?
    yes, the reload will cause you a problem
    I don't see how. I have output buffering set to 0 in my php.ini file, and the following code works fine for me. Enter something in the text box and hit submit, then do it again.

    atest2.php:
    PHP Code:
    <?php 
    session_start
    ();
    if(isset(
    $_POST['color']))
    {
        
    $_SESSION['colorArray'][] = $_POST['color'];
        
    print_r($_SESSION['colorArray']);
    }
    ?>
    <html>
    <head><title></title>
    </head>

    <body>

    <div>atest2.php</div>
    <form name="f" method="post" action="atest2.php">
        <input type="text" name="color" />
    <input type="submit" value="Submit" />
    </form>

    </body>
    </html>
    You just have to make sure there are no blank lines above your php tag at the top of the page:
    Code:
    <----
    <?php
    session_start();
    ?>
    and there are no spaces preceding the tag:
    Code:
    *<?php
    session_start();
    ?>

  5. #5
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,804
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    I see what you mean but it does depend on if there is a ceck for if(isset($_POST[....])) etc.

    Depends on the codeing really

    Mike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  6. #6
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ob_start() isnt going to fix this if she already said session_start() is at what she thinks is the top of the file. that would just start output buffering after she already generated output, which wouldnt accomplish anything.


    alison, you must have some kind of output before your
    <?php
    session_start();
    ?>


    this may be a space, or a blank line, or maybe you used include() to include another file which produces output. but you cannot have any output of any kind before you call session start.

    that means the VERY first thing in your php file must be
    <?php

    no spaces or blank lines are allowed to come before that.

  7. #7
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spikeZ
    I see what you mean but it does depend on if there is a ceck for if(isset($_POST[....])) etc.

    Depends on the codeing really

    Mike
    I'm afraid that's incorrect. If isset() is not tested, the code won't work because it will produce an undefined error, but you will not get an error because headers have already been sent.

  8. #8
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,804
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    You know what 7stud, you are quite right - I really need to unplug and lie in a darkened room for a while!
    I am big enough to admit my mistakes!

    Mike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  9. #9
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am big enough to admit my mistakes!
    No worries.


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
  •