Reputation: 1863
When I print_r($_COOKIE);
, I get following result.
Array ( [filters] => Array ( [input1] => 1 [input2] => 20000 [input3] => none ) [PHPSESSID] => 12334 )
I want to delete some element and want it to be like this:
Array ( [filters] => Array ( [input3] => none ) [PHPSESSID] => 12334 )
I tried this but it not effecting anything in $_COOKIE
$past = time() - 3600;
setcookie( "filters[input1]", "", $past, '/' );
setcookie( "filters[input2]", "", $past, '/' );
What is wrong here? All day is waisted trying this?
Thanks
Upvotes: 0
Views: 166
Reputation: 3189
If you created your cookies with a domain , you may need to remove then using the same domain name. i.e.
to set a cookie:
setcookie('mycookie', 'value', time() + 999, '/', '.my.domain', false);
to delete cookie:
setcookie('mycookie', "", -1, '/', '.my.domain', false);
Upvotes: 0
Reputation: 9740
Are you sure that you call setcookie()
to delete the cookie with the same arguments (path, secure, etc) as you did to create the cookie?
Also, setcookie()
does not affect $_COOKIE
in the running script. Only subsequent calls to that script will have the modified $_COOKIE
array. To remove values from $_COOKIE
in the same session, call unset($_COOKIE['name'])
.
Upvotes: 2
Reputation: 1126
You must reload the page after your run setcookie() - the result of your setting cookie is not available until you reload the page. Also try using 'older' time - older than 1 hour - try like a year in the past. Some browsers will not delete cookie if the time is not far enough in the past
Upvotes: 1
Reputation: 2591
try:
unset($_COOKIE['filters']['input1']);
unset($_COOKIE['filters']['input2']);
$time = time() + 1000; // enything you want, if its in the past $_COOKIE['filters'] will no loger exist
setcookie('filters', $_COOKIE['filters'], $time, '/' );
Upvotes: 1