Welcome to PHP Smarty Q&A, where you can ask programmaing questions and receive answers from other members of the community for PHP, Smarty, MYSQL related. You can also share or save your scripts in this repository for future reference.

Destroying a specific cookie in PHP

+1 vote
107 views

I am developing and learning PHP and I want to destroy a cookie which has been created for 1 month.

<?php

if (!isset($_COOKIE['myiser'])) {
    setcookie('myiser', true, time() + 86400 * 30); // Save a cookie for 30 days
    echo '<div class="mydiv">hello world!</div>';
}

?>

Can you please help in destroying this cookie.

asked Sep 20, 2015 in PHP by geeta

1 Answer

0 votes
 
Best answer

Google has so many answers in this related, anyway, you can unset and remove stored cookies from your user system as follow

<?php
	if (!isset($_COOKIE['myiser'])) {
		setcookie('myiser', true, time() + 86400 * 30); // Save a cookie for 30 days
		echo '<div class="mydiv">hello world!</div>';
	}

	if (isset($_COOKIE['myiser'])) {
		//lets unset the cookie
		unset($_COOKIE['myiser']);
		//lets empty the cookie value and expire it with old time stamp
		setcookie('myiser', '', time() - 3600 * 24, '/');

	}
?>

Note : You can change -3600 * 24 into something old date if its more than 1 day old, you mentioned, you are under development, hence, I wrote for 24 hours.

answered Sep 20, 2015 by robert (13,210 points)
...