Page 1 of 2

Unsetting An Array in the Cookie Array

Posted: Mon Apr 23, 2007 2:20 am
by nyfael
Hey Everyone,

My cookie array looks like this:

Code: Select all

Array
(
    [items] => Array
        (
            [aw1] => Array
                (
                    [item_name] => Article Writing
                    [x] => 53
                    [orig_price_aw1] => 70
                    [cb_aw1_33_0] => Cool
                    [item_price] => 100000070
                    [form_type] => item
                    [y] => 24
                    [sel_price_aw1] => 100000000.00
                    [31] => Boogy is awesome
                    [quantity] => 1
                    [form_name] => aw1
                )

        )

)
I want to remove the [aw1] array in the cookie set, and I can't seem to get it to work, I've tried a variety of methods. One thing, there will be more arrays besides the [aw1] which is why I'm not unsetting ALL cookies, which I have been able to do successfully, but using another premade loop:

Code: Select all

if (isset($_SERVER['HTTP_COOKIE'])) {	// unset cookies
	$cookies = explode(';', $_SERVER['HTTP_COOKIE']);
	foreach($cookies as $cookie) {
		$parts = explode('=', $cookie);
		$name = trim($parts[0]);
		setcookie($name, '', time()-1000);
		setcookie($name, '', time()-1000, '/');
	}
}
Thanks,
Kerry

Posted: Mon Apr 23, 2007 8:57 am
by feyd
why not use unset() and $_COOKIE?

Posted: Mon Apr 23, 2007 10:33 am
by pickle
I for one don't like messing with superglobals directly. In this case, I'm not sure calling unset() will remove the cookie from the clients machine.
PHP Docs wrote:Cookies must be deleted with the same parameters as they were set with. If the value argument is an empty string, or FALSE, and all other arguments match a previous call to setcookie, then the cookie with the specified name will be deleted from the remote client.

Posted: Mon Apr 23, 2007 11:19 am
by nyfael
Unset won't unset a cookie, cookies at php.net shows you how to unset one, but I couldn't get it to work on an individual array.

Also, I can unset the entire $_COOKIE, but I want to remove an array in the $_COOKIE.

Thanks,
Kerry

Posted: Mon Apr 23, 2007 11:23 am
by pickle
So you've got an array of data stored in one cookie & you want to unset a sub-array of that? I think you'll have to just re-set the array with that sub-array missing.

Posted: Mon Apr 23, 2007 12:04 pm
by Burrito
unset() does not work for cookies. You'll have to send a new cookie with an expiration in the past to kill the cookie entirely. If you just want to remove a sub-index, you'll have to resend the cookie with only the info you want in the cookie.

Posted: Mon Apr 23, 2007 1:55 pm
by feyd
:oops: I forgot PHP doesn't automatically serialize the cookie information.

Simple enough to fix.

Posted: Mon Apr 23, 2007 2:37 pm
by nyfael
@Burrito:

Could you give me an example of how to do that? I tried it and it didn't really work...

-Kerry

Posted: Mon Apr 23, 2007 2:53 pm
by Burrito
to do which?

Posted: Mon Apr 23, 2007 3:02 pm
by nyfael
The array I gave of my cookies above - could you show me how to unset aw1? The entire array? Assuming I had other arrays like aw1 in the $_COOKIE array.

Posted: Mon Apr 23, 2007 3:21 pm
by feyd

Code: Select all

<?php

if(isset($_COOKIE['items']) and count(unserialize($_COOKIE['items'])) > 0)
{
	$items = unserialize($_COOKIE['items']);
	unset($items['awl1']);
	setcookie('items', serialize($items));
	setcookie('tested', serialize(true));
	echo '<html><head></head><body><a href="', $_SERVER['PHP_SELF'], '">load again to finish.</a>', var_export($_COOKIE,true), '</body></html>';
}
elseif(isset($_COOKIE['tested']))
{
	echo 'Cookie test complete.';
	var_export($_COOKIE);
}
else
{
	setcookie('items', serialize(array('awl1'=>array('foo'=>'bar'))) );
	echo '<html><head></head><body><a href="', $_SERVER['PHP_SELF'], '">load again to test.</a>', var_export($_COOKIE,true), '</body></html>';
}

?>

Posted: Mon Apr 23, 2007 4:54 pm
by nyfael
Hey, that would work if I had a serialized array but I don't, I actually made an array:

Code: Select all

setcookie("items[aw1]", $value, $time);
Have any technique for that?

Posted: Mon Apr 23, 2007 7:01 pm
by superdezign
I don't really have a solution for you (I thought you could just use setcookie() and reset the cookie's value to what you wanted it to be), but I just figured I'd throw in my two cents.

I avoid cookies because of the fact that they can be altered client-side. I'm not sure what you need it for, but it doesn't seem like a simple "Save Username," as you ARE using an array. Maybe a different method of storage would help solve the problem? Is a session too short?

And by the way, when saving variables you should definitely serialize them to ensure they are properly saved.

Posted: Mon Apr 23, 2007 7:01 pm
by nyfael
Hey - I figured out the problem. I didn't fully understand how Cookies were set, so I decided reading from PHP.net.

I neglected to tell you (had no idea it was relevant) that I was using an ajax function to call this file in another folder... due to the path specification of a Cookie, it is restricted to the current directory unless specified. My solution was to set the cookie globally to the current domain, and delete them. My final deleting code looks like this:

Code: Select all

if(isset($_COOKIE['items']) and is_array($_COOKIE['items']))
{
	foreach($_COOKIE["items"][$_GET["CID"]] as $key => $value)
	{
		$result[$key] = setcookie("items[" . trim($_GET["CID"]) . "][" . trim($key) . "]", "", time() - 3600, "/");
	}
}
Thanks for all your help!;

Posted: Tue Apr 24, 2007 7:17 am
by feyd
You really shouldn't be setting cookies with the array naming format. Browsers are typically set to only accept a maximum of four cookies from any given domain. You could easily run into an issue where your cookie data is being deleted.