Reputation: 64
I have a problem clear cookie when session is destroyed or expired. I create application with session (logged user) and cookies (cart) and I must clear cart when session is destroyed or expired. I'm tring use $_SESSION[SESSION_NAME], but not always $_SESSION[SESSION_NAME] has been removed after destroy or expire.
if(!isset($_COOKIE[SESSION_NAME])) {
clearCookie();
}
When I remove cookie with session ID then cookies has been cleared, but when session expire then cookie (and cookie with session ID) have been existed.
I'm tried all session functions (https://www.php.net/manual/en/ref.session.php), but I'm not found good solution. How to clear cookie when session has been removed or expired?
Upvotes: 0
Views: 1958
Reputation: 178
set cookie value to the past date
setcookie($cookie_name, '', time() - (86400 * 30), "/",""); // 86400 = 1 day
session_destroy();
Upvotes: 0
Reputation: 4089
As your cookie is identified by data from your session, it would be logical to delete/clear the cookie before destroying the session.
So, whenever you need to destroy the session, first clear the cookie, then destroy your session.
Something like this:
clearCookie();
unset($_SESSION);
session_destroy();
Upvotes: 0