Unsetting An Array in the Cookie Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

nyfael
Forum Commoner
Posts: 32
Joined: Thu Sep 21, 2006 2:28 am

Unsetting An Array in the Cookie Array

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

why not use unset() and $_COOKIE?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
nyfael
Forum Commoner
Posts: 32
Joined: Thu Sep 21, 2006 2:28 am

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

:oops: I forgot PHP doesn't automatically serialize the cookie information.

Simple enough to fix.
nyfael
Forum Commoner
Posts: 32
Joined: Thu Sep 21, 2006 2:28 am

Post by nyfael »

@Burrito:

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

-Kerry
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

to do which?
nyfael
Forum Commoner
Posts: 32
Joined: Thu Sep 21, 2006 2:28 am

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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>';
}

?>
nyfael
Forum Commoner
Posts: 32
Joined: Thu Sep 21, 2006 2:28 am

Post 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?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
Last edited by superdezign on Mon Apr 23, 2007 7:02 pm, edited 1 time in total.
nyfael
Forum Commoner
Posts: 32
Joined: Thu Sep 21, 2006 2:28 am

Post 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!;
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply