+ Post New Thread
Results 1 to 7 of 7
Web Development Thread, Cookies in PHP in Coding and Web Development; Again feeling a bit of an idiot for having to ask this but I'm trying to work with Cookies and ...
  1. #1
    demsley's Avatar
    Join Date
    Feb 2007
    Location
    Sheffield, UK
    Posts
    29
    Thank Post
    19
    Thanked 2 Times in 2 Posts
    Rep Power
    16

    Cookies in PHP

    Again feeling a bit of an idiot for having to ask this but I'm trying to work with Cookies and having problems.

    Code should work AFAICS but no;

    ------------------------------------------
    <?php
    //Check and Increment Cookie
    if(!isset($_COOKIE['Counter'])) {
    echo "top section<br />";
    $Counter = $_COOKIE['Counter']+1;
    setcookie("Counter", $Counter );
    echo "Setting Cookie to " . $Counter . "<br />";
    }
    else {
    echo "Cookie not set";
    setcookie("Counter", "1" );
    $Counter = 1;
    }
    ?>
    <?php
    // Print an individual cookie
    echo $_COOKIE["Counter"];
    echo $HTTP_COOKIE_VARS["Counter"];

    // Another way to debug/test is to view all cookies
    print_r($_COOKIE);
    ?>


    <html>
    <body>

    <?php
    // Print individual cookies
    echo "You have visited " . $Counter . " times ";


    ?>

    </body>
    </html>
    -------------------------------------
    The page is available at: http://www.emsley.org.uk/poll/testcookies.php

    Any ideas would be gratefully received.

    Cheers

    Dave

  2. #2

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    17,694
    Thank Post
    516
    Thanked 2,455 Times in 1,899 Posts
    Blog Entries
    24
    Rep Power
    833
    Your if !isset is running the first section if the cookie is NOT set, and the else if it IS set. So they're backwards. Get rid of the !

  3. Thanks to localzuk from:

    demsley (2nd September 2011)

  4. #3

    CESIL's Avatar
    Join Date
    Nov 2006
    Location
    Hampshire
    Posts
    1,404
    Thank Post
    109
    Thanked 267 Times in 198 Posts
    Rep Power
    168
    What he said!

  5. Thanks to CESIL from:

    demsley (2nd September 2011)

  6. #4

    ZeroHour's Avatar
    Join Date
    Dec 2005
    Location
    Edinburgh, Scotland
    Posts
    5,709
    Thank Post
    906
    Thanked 1,324 Times in 805 Posts
    Blog Entries
    1
    Rep Power
    446
    BTW unless you buffer you cant output/echo until you set your cookies. Any *output* ends the chance to set cookies so remove/move the echo's after the SetCookie.
    Normally an error would be in the logs for that.

  7. Thanks to ZeroHour from:

    demsley (2nd September 2011)

  8. #5
    demsley's Avatar
    Join Date
    Feb 2007
    Location
    Sheffield, UK
    Posts
    29
    Thank Post
    19
    Thanked 2 Times in 2 Posts
    Rep Power
    16
    Thanks for the responses, I've taken out the echos I'd put in but it doesn't work still. I'm feeling very stupid at this stage and know there's something I've missed - but what it is I have no clue!

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <?php
    //Set Cookie
    if(isset($_COOKIE['Counter'])) {
    $Counter = $_COOKIE['Counter']+1;
    setcookie("Counter", $Counter );
    }
    else {
    setcookie("Counter", "1" );
    $Counter = 1;
    }
    ?>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <body>

    <?php
    // Print individual cookies
    echo "You have visited " . $Counter . " times ";


    ?>

    </body>
    </html>

  9. #6

    CESIL's Avatar
    Join Date
    Nov 2006
    Location
    Hampshire
    Posts
    1,404
    Thank Post
    109
    Thanked 267 Times in 198 Posts
    Rep Power
    168
    Your Doctype is classed as output so you need to move this to after the cookie code above the xml line...

  10. #7

    ZeroHour's Avatar
    Join Date
    Dec 2005
    Location
    Edinburgh, Scotland
    Posts
    5,709
    Thank Post
    906
    Thanked 1,324 Times in 805 Posts
    Blog Entries
    1
    Rep Power
    446
    Quote Originally Posted by CESIL View Post
    Your Doctype is classed as output so you need to move this to after the cookie code above the xml line...
    Yep the php code for cookies needs to literally be the first thing at the top of the file.

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 3
    Last Post: 5th July 2009, 12:07 AM
  2. Concatenate in PHP
    By Hightower in forum Web Development
    Replies: 26
    Last Post: 18th November 2008, 01:18 PM
  3. Query of existing query data in PHP
    By markwilliamson2001 in forum Web Development
    Replies: 5
    Last Post: 5th October 2007, 08:43 AM
  4. Creating a new database in php and mysql
    By StewartKnight in forum Web Development
    Replies: 18
    Last Post: 21st February 2007, 08:53 AM
  5. CLEO blocking sendmail in PHP?
    By woody in forum Web Development
    Replies: 11
    Last Post: 20th October 2006, 08:28 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •