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!
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:
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.
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.
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.
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.
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.
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:
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.