简体   繁体   中英

getting an array of php cookies based on cookie name

I'm not sure if this is even possible, but I am trying to do a favourites list (using cookies). I can add/delete cookies using jquery and print out my cookies list using php fine. However, my cookie array has some things which I'm not interested in and I think some are variable so I can't just unset it manually.

Here's my cookie array:

Array
(
    [ys-vmconfiguration-active] => s:ext-comp-1005
    [94106e70fc3234ce511b7b9bd5d5d9b0] => 639b623be5a68913a11421e23de05559
    [6fb7ca7b058abd27041a8b41fed8f368] => 54 D575B4211 B 8 E4110165741 D 2 B 610 3415F 45914 050 B A F12 A46 D5D5B1B1250434714 A17561158105C56 81A53 15C A5843 F1B
    [d5e8a24a0ca709537c5c26b3134af3b5] => 46aac9d9a4d6c6a1a74c529a43dd4187
    [item_225] => 225
    [item_6] => 6
    [item_123] => 123
    [item_19] => 19
)

^^ Is there any way of getting all of the cookies that start with "item_" only?

So my new cookie array would be:

Array
(
    [item_225] => 225
    [item_6] => 6
    [item_123] => 123
    [item_19] => 19
)
function has_item($key) {
   return strpos($key, 'item_') !== false;
}
$_COOKIE = array_flip(array_filter(array_flip($_COOKIE), 'has_item'));

I tested this out using a php file on the command line and it worked. As for whether it will work with the browser, I can't say for sure, but I don't see why not. Another thing to consider is: is having those other cookies that bad? They are probably harmless tracking cookies.

Anyway, an explanation: flip keys with values so the values are maintained. You cannot filter by keys, only values. Filter items that do not have "item_". Then flip it back again so the keys and values are correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM