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!
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.
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.
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.
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.
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.
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.
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.
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?
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.