Can't clear cookies in PHP 5

Discussion in 'PHP' started by phantom, Oct 29, 2007.

  1. #1
    I have this working code:

    $time = mktime(12,0,0,1, 1, 1970);
      setcookie ("cookiename", "", $time - 3600);
    Code (markup):

    that works under PHP4 to clear (or reset) a cookie

    But it does not work on PHP5


    I have tried many variations to clear the cookie ...all of which work as expected on PHP4 but not on PHP5


    Anyone have any idea why? Is there something else that must be done in PHP5?

    Thanks!
     
    phantom, Oct 29, 2007 IP
  2. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    You don't need to set the time so far back in the past, but that shouldn't be an issue because it's still a valid time (unless if the browser for some reason ignores it because it's so far back, but it's quite unlikely).

    Try these:
    1. Just to be sure, set the expiry time to: time() - 3600 instead of mktime(12, 0, 0, 1, 1, 1970) - 3600.
    2. Make sure nothing is output before setcookie() is called. Echoing something, a notice/error message being thrown by PHP or even blank space before the <?php tag could cause setcookie() to not work.
    3. Check the return value of setcookie(), make sure it returns 'true'.
    4. If you haven't already, set your PHP to output error messages (error_reporting to E_ALL, display_errors and display_startup_errors to 'On') and see if it gives you any useful error messages.

    That's all I can think of at the moment. This is not an issue of setcookie() working differently in PHP5/PHP4.
     
    phper, Oct 30, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    Actually, he's using mktime(12, 0, 0, 1, 1, 1970) - 3600, which is one hour prior the UNIX epoch. Some browsers or servers might not understand this. I think PHP 5 should be able to deal with negative timestamps though.

    I disagree here. Chances that the browser and the server are in different timezones are very high.

    Let's for example assume the server is located in Europe, or is set to the European timezone. Now an American user visits your site and receives the cookie which supposed to expire one hour prior the current server date.

    The cookie would not be deleted, but it would be overwritten by the new one, and it would remain 5 more hours as there are 6 hours time difference between Europe and America.

    So you're best off setting the expiry date to -1 or -2 days.

    
    setcookie("cookiename", "0", strtotime('-2 Days'));
    
    PHP:
    (I always give cookies a value as well, in case some browsers don't like empty cookies)

    If that still doesn't work, try changing the error reporting to E_ALL as suggested above.
     
    nico_swd, Oct 30, 2007 IP
  4. tonybogs

    tonybogs Peon

    Messages:
    462
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Another option to check is how the cookie is being set.

    If the cookie is set to a your domain, say .example.com and you are trying to delete it using your technique at www.example.com it wont work.
     
    tonybogs, Oct 30, 2007 IP
  5. gordi555

    gordi555 Active Member

    Messages:
    537
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Yes and another would be to check directories where the cookies are stored (just incase).
     
    gordi555, Oct 30, 2007 IP
  6. phantom

    phantom Well-Known Member

    Messages:
    1,509
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    140
    #6
    Ok guys thanks for all the help...........I got it figured out.....
     
    phantom, Oct 30, 2007 IP
  7. gordi555

    gordi555 Active Member

    Messages:
    537
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #7
    How did you fix it? Just for other readers if they have the same problem.
     
    gordi555, Oct 30, 2007 IP
  8. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #8
    Epoch time is 1 Jan 1970 00:00:00, the time in the mktime() function above is actually 12 hours past that :)

    When the server sends a Set-cookie header to the browser, the expiration time is always set in GMT. So we don't need to worry about the different time zones.

    It's still possible however that the user's computer's clock is incorrect and causing incorrect cookie expiration issues, but this is beyond what we can deal with on the server side.
     
    phper, Oct 30, 2007 IP
  9. phantom

    phantom Well-Known Member

    Messages:
    1,509
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    140
    #9
    My problem was is that I had recently set my domain as wildcarded so the cookie being set was for a subdomain. So when I tried to clear it it was trying to clear the wrong one.
     
    phantom, Oct 30, 2007 IP
  10. gordi555

    gordi555 Active Member

    Messages:
    537
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #10
    Thanks for sharing.
     
    gordi555, Oct 31, 2007 IP
  11. Ri Genette

    Ri Genette Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Sorry, phantom.

    I 've read your article very carefully and they are helpful for me.
    So, I have one question, when you were trying to set cookie, did't have any problem?

    Although I used set cookie function, but always returns False.
    I really cann't set cookie.

    Can you help me, phantom?

    expecting advice.
    Thank you
    genette.
     
    Ri Genette, Oct 31, 2007 IP
  12. phantom

    phantom Well-Known Member

    Messages:
    1,509
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    140
    #12
    hmm I never had problems setting them......just erasing/clearing them.

    Maybe you can post your code that way someone can see exactly what is wrong with it so you can get help.
     
    phantom, Oct 31, 2007 IP
  13. Ri Genette

    Ri Genette Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
     
    Ri Genette, Oct 31, 2007 IP
  14. phantom

    phantom Well-Known Member

    Messages:
    1,509
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    140
    #14
    Here is one of my cookie setting codes
    setcookie("cookiename", $cookie_data, time() + (60*60*24));
    Code (markup):
    Your problems might have to do with what is in your array $refl[0][3]

    But I don't know for sure......do you know what is in that array?
     
    phantom, Nov 3, 2007 IP
  15. KiruSoft

    KiruSoft Peon

    Messages:
    97
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    simply
    
    unset($_COOKIE['mycookie']);
    
    PHP:
     
    KiruSoft, Nov 5, 2007 IP
  16. phantom

    phantom Well-Known Member

    Messages:
    1,509
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    140
    #16
    kiruSoft
    thanks however
    that doesn't work with my setup since I am setting the cookie for a wild carded domain I have to unset it for .domain.com .........notice the leading dot.

    Not sure what is going on with Ri Genette so it may work for them.
     
    phantom, Nov 5, 2007 IP