SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Enthusiast rmacca's Avatar
    Join Date
    Dec 2000
    Location
    Hamilton, New Zealand
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Sessions Error

    Hey ppl,

    I'm having a problem with getting sessions work on my server.
    i get the following error everytime:


    Warning: Cannot send session cookie - headers already sent by (output started at D:\apache\htdocs\sessiontest.php:13) in D:\apache\htdocs\sessiontest.php on line 15

    Warning: Cannot send session cache limiter - headers already sent (output started at D:\apache\htdocs\sessiontest.php:13) in D:\apache\htdocs\sessiontest.php on line 15


    My code is simply a sessiontest:

    PHP Code:
    <?php

    session_start
    ();

    // session variable to store the counter.
    session_register('counter');

    // session variable to store the value when the page was last loaded;
    // this value is maintained so that difference can be calculated.
    session_register('timeAtLastLoad');

    // current time
    $timeNow time();

    // increment counter
    $counter++;

    // calculate the time lapsed from last visit.
    $timeLapsed $timeNow $timeAtLastLoad;

    // display appropriate message
    if($counter 1)
    {
        echo 
    "<b>It's been $timeLapsed seconds since you last viewed this page.</b>";
    }
    else
    {
        echo 
    "<b>First time here? Reload this page to see how the session  works!</b>";

    }

    $timeAtLastLoad $timeNow;

    ?>
    Can someone help me?

  2. #2
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Put the line
    $timeAtLastLoad = $timeNow;
    before the if statement to see if it works.

    John

  3. #3
    SitePoint Enthusiast rmacca's Avatar
    Join Date
    Dec 2000
    Location
    Hamilton, New Zealand
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That made no difference. I should probably add a bit more detail to my problem now. I'm running the latest version of phpTriad (2.11), I used to run version 1.3 and my sessions script ran using that. But since I updated sessions no longer work. I think it may be to do with the php setup. Does anyone no what may cause this?

  4. #4
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No single white space before session_start statement. Try that.

    John

  5. #5
    blonde.... Sarah's Avatar
    Join Date
    Jul 2001
    Location
    Berkshire, UK
    Posts
    7,442
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    do you have HTML with this script? if so you need to have this BEFORE the HTML tags as they send header info and you can only send it once.

    Otherwise white space is the answer

    Sarah

  6. #6
    SitePoint Enthusiast rmacca's Avatar
    Join Date
    Dec 2000
    Location
    Hamilton, New Zealand
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope... neither of those made ne difference... im fairly sure its 2 do with the php settings...

    I need more help ppl. Thanks to johnn & sarah...
    L8az ppl

  7. #7
    SitePoint Addict kunal's Avatar
    Join Date
    Oct 2000
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    make sure there are no spaces or between the first <? and the session_start(); command.. there should be nothing.. no space.. no nothing...
    i dunno...

  8. #8
    Victory shall be mine tubedogg's Avatar
    Join Date
    Mar 2001
    Location
    Medina, OH
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There can be 5000 blank lines as long as they aren't before the <?php. But after that it doesn't matter.
    Kevin

  9. #9
    SitePoint Addict
    Join Date
    Jun 2001
    Location
    Malaysia
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had the exact same problem.. then I tried putting my session_start() in the HTML header:

    PHP Code:
    <?php session_start(); ?>
    <HTML>
    ....
    <? the rest of your code follows...

    ?>
    declare session_start() at the VERY top of the page. BEFORE anything else.. even before HTML if you're using it.

  10. #10
    Victory shall be mine tubedogg's Avatar
    Join Date
    Mar 2001
    Location
    Medina, OH
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Exactly. Any header-type functions, such as sessions and/or cookies, need to be done and sent to the client before *anything* else. The reason you can have blank lines after the <?php is because after the open PHP tag, the server takes over and parses it before anything is sent to the client. The whole script is parsed before being sent - unless there is anything echoed, printed, HTML, etc. in the script in which case it is sent then the server continues.
    Kevin

  11. #11
    SitePoint Addict
    Join Date
    Jun 2001
    Location
    Malaysia
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yep tubedogg!

    took HELL of a lot of warning msgs before I figured that one out!!

  12. #12
    SitePoint Enthusiast rmacca's Avatar
    Join Date
    Dec 2000
    Location
    Hamilton, New Zealand
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yep that worked... thanks. I put the session start right at the top and it works now...

    l8az, rmacca

  13. #13
    SitePoint Zealot cokeman's Avatar
    Join Date
    Dec 2000
    Location
    So. California
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This may or may not be useful:
    PHP Code:
    <?php 
    ob_start
    (); 

    echo 
    "hello"
    header("someheader here"); 
    ob_end_flush(); 
    ?>


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
  •