SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Setting and Removing Cookies.

    Hello guys, I'm a beginner at PHP.

    I have created cookie.php:

    Code:
    <!DOCTYPE html> 
     
    <?php
    
    if(!isset($_COOKIE['myCookie'])){
        $welcome = "Hello New Guy!";
        setcookie('myCookie', 1, time()*7*365);
    }
    else{
        $welcome = "Welcome back!";
    }
    
    ?>
     
    <html>
    <head>
    	<meta charset="utf-8">
    	<title>Cookie.php</title>
    </head>
    
    <body>
        
        <div>
            <?php if(isset($welcome)) echo $welcome; ?>
            <a href="removeCookie.php">Remove Cookie</a>
        </div>
        
    </body>
    </html>
    Note: There is an anchor to removeCookie.php right below the div.

    Here is the removeCookie.php:

    Code:
    <?php
    
    setcookie('myCookie', 1, time() - 3600);
    header("location: cookie.php");
    
    ?>
    Can anyone spot the error?

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Could you explain what the issue is that your having?
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Likewise, not sure what the Q is, but this line is surely wrong:
    PHP Code:
    setcookie('myCookie'1time()*7*365); 
    SB
    PHP Code:
    setcookie('myCookie'1time() + (86400*365) ); 
    This is if the idea is to add one year of life to the cookie.

    The idea being

    take the number of seconds up to now - time()
    to that add +
    the number of seconds in a a day - 86400
    and multiply by - *
    the number of days in a year - 365

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi guys. Sorry for not being clear.

    This just doesn't work. The output is "Hello New Guy!" and remains so, regardless of how many times I refresh the page.

    Is this because the cookie has not been set, or perhaps I am not receiving the cookie correctly?

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    It seems that as soon as you delete the cookie, you send the user back round to get another one so the condition is always met.

    If I click on "delete cookie", why are you sending me back to get another?

    Why don't you tell us what you want to happen, fill the idea out a bit - then someone can help you.

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    It seems that as soon as you delete the cookie, you send the user back round to get another one so the condition is always met.

    If I click on "delete cookie", why are you sending me back to get another?

    Why don't you tell us what you want to happen, fill the idea out a bit - then someone can help you.
    Hi Cups,

    Thanks for reply. I am now learning about the behaviour of cookies, and I am experimenting.

    I am following the screencast tutorial by Jeffrey Way: In the Woods – Diving into PHP: Day 9

    This is what I was hoping to do with the first block of code:

    Code:
    if(!isset($_COOKIE['myCookie'])){
        $welcome = "Hello New Guy!";
        setcookie('myCookie', 1, time() + (86400*365));
    }
    else{
        $welcome = "Welcome back!";
    }
    1. Check if there is an existing cookie 'myCookie' in the browser.
    2. If myCookie DOES NOT exist, then the $welcome = "Hello New Guy!", and a cookie will be set.
    3. If myCookie DOES exist then the $welcome = "Welcome Back!"

    As you can see, the $welcome variable has two different values depending on whether the cookie exists or not. And I output the $welcome as such:

    Code:
        <div>
            <?php if(isset($welcome)) echo $welcome; ?>
            <a href="removeCookie.php">Remove Cookie</a>
        </div>
    I have also linked to the page removeCookie.php, where the cookie may be removed:

    Code:
    <?php
    setcookie('myCookie', 1, time() - 3600);
    header("location: cookie.php");
    ?>

  7. #7
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Have you tried visiting the page, and then checking your cookies to see if you did, in fact, get the cookie?

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    how about you put this as the first line in PHP
    PHP Code:
    <?php

    if( isset( $_COOKIE )){
    var_dump$_COOKIE );
    }

    // then do your conditional checks...
    ?>

  9. #9
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    how about you put this as the first line in PHP
    PHP Code:
    <?php

    if( isset( $_COOKIE )){
    var_dump$_COOKIE );
    }

    // then do your conditional checks...
    ?>
    var_dump ($_COOKIE); returns the following

    Code:
    array(1) { ["SQLiteManager_currentLangue"]=> string(1) "2" }
    So I guess there is, in fact, a cookie in existence?

  10. #10
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Well when you refresh the page, it should show your cookie in there (alongside that one you showed us) when you visit a page after you have deleted it, it should not be there.

    To focus in on this issue, create 3 different pages, 1,2,3.

    1 sets the cookie
    2 reads the cookie
    3 deletes the cookie

    All with var_dump() at the top of the page, also depending on your browser install some "developer tools" so you can see the content of any given cookie at any time.

    Try this tutorial if you can make no headway, and listen, cookies can be a hard thing to grasp, I remember trying to dig them when I first encountered Javascript - took me flippin' ages.

    Once you get it it is easy to work out what you are doing wrong. (Having said that, be warned that localhost is not the same host as 127.0.0.1 and I have run into issues with IE not reading cookies set on localhost, 'twas years ago though)


Tags for this Thread

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
  •