SitePoint Sponsor

User Tag List

Results 1 to 20 of 20
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    scarborough
    Posts
    139
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Real problem with session code

    I am having awful trouble with sessions. I have copied the following code into a page and uploaded it. It comes back with the following warning messages

    Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (session1.php:1) session1.php on line 5

    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent


    Here is the code. Exactly what am I doing wrong as I copied and pasted it and hacve tried other examples and its uts not working

    PHP Code:
    <?php
    session_start
    (); 
    $_SESSION['views'] = 1// store 

    session data
    echo "Pageviews = "$_SESSION

    ['views']; //retrieve data
    ?>

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    <?php
    session_start();
    $_SESSION['views'] = 1; // store

    session data
    echo "Pageviews = ". $_SESSION

    ['views']; //retrieve data
    ?>

    You forgot to comment out that line?

    and then... have some

    random enter strokes.

    Random enter strokes = broken things.

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    scarborough
    Posts
    139
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry

    This is the code

    PHP Code:
    <?php
    session_start
    ();  
    if(isset(
    $_SESSION['views']))
    $_SESSION['views'] = $_SESSION['views']+ 1;
    else 
    $_SESSION['views'] = 1;
    echo 
    "views = "$_SESSION['views']; 
    ?>
    Its cut and pasted from an example from the internet. It gets the same warning message as I mentioned. It does not increment as it should do

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    session_start
    ();
    if( ! isset(
    $_SESSION['views'])){
      
    $_SESSION['views'] = 1;
    }else{
      
    $_SESSION['views']++;
    }

    echo 
    'You viewed this '$_SESSION['views'], ' times.';
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    scarborough
    Posts
    139
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Anthony

    I cut and pasted your code with the same warnings and it doesn't work.

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    The exact same warning? Place the code I provided in a script by itself, you shouldn't have an issue.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Make sure there's no whitespace, no blank lines, nothing before the <?php, as well.

  8. #8
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Er... actually... Anthony's code needs a slight modification....

    echo 'You viewed this '. $_SESSION['views']. ' times.';

  9. #9
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    Er... actually... Anthony's code needs a slight modification....

    echo 'You viewed this '. $_SESSION['views']. ' times.';
    No it doesn't.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  10. #10
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,014
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Are you sending any output in any scripts before this one?
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  11. #11
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    scarborough
    Posts
    139
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    I just opened the page in wamp server. So far I have been putting everything out onto the internet. Its works perfectly on wamp but on myt webspace it does not work and sends back these warnings.

  12. #12
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    scarborough
    Posts
    139
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Session code works with wamp but not on internet

    I have the following session script which works on Wamp server.However it fails to work on the internet... why

    PHP Code:
    <?php
    session_start
    ();
    if( ! isset(
    $_SESSION['views'])){
      
    $_SESSION['views'] = 1;
    }else{
      
    $_SESSION['views']++;
    }

    echo 
    'You viewed this '$_SESSION['views'], ' times.';
    ?>

    It comes up with warnings that headers have been sent.

  13. #13
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    What is the error?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  14. #14
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    [Threads Merged]
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  15. #15
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    scarborough
    Posts
    139
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    These are the warnings

    Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (session1.php:1) session1.php on line 5

    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent

    AND the program doesn't work. The variable stays at 1 and never increments.

    IT WORKS with wamp server but on the internet it fails. Its probably something to do with my web hosts.

  16. #16
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You're not using the code I posted, if you we're, the error would be on line 1 not 5. If you post the actual code you're using, we can try to help you.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  17. #17
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    scarborough
    Posts
    139
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry - I copied and pasted the errors from the post earlier. Here are the errors in full

    Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/castcou7/public_html/dreamsymbolism/forum/session1.php:1) in /home/castcou7/public_html/dreamsymbolism/forum/session1.php on line 2

    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/castcou7/public_html/dreamsymbolism/forum/session1.php:1) in /home/castcou7/public_html/dreamsymbolism/forum/session1.php on line 2
    You viewed this 1 times.

    The code does work - there i nothing wrong with the code. It works on wamp server. So there is something about my server that is not working.

  18. #18
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,014
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    What version of PHP is running on the servers?
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  19. #19
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    If looks like the script you have may need the BOM removing, your editor should be able to this for you.

    This should prove useful.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  20. #20
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    scarborough
    Posts
    139
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks

    I always save in utf-8. I thought that was problem free. I saved this again as ansi and it worked first time

    How do I get rid of BOM's


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
  •