troubleshooting Question

How to add a value to an existing cookie separated by a comma?

Avatar of evibesmusic
evibesmusicFlag for United States of America asked on
Web DevelopmentPHP
11 Comments1 Solution2308 ViewsLast Modified:

The following script checks to see if a cookie exists.  If the cookie does exist, it checks the value of $ID and compares it to the value already stored in the cookie.  If the values are different, it adds the value of $ID to the cookie.

I am trying to get the cookie to store the values as a comma delineated list.
Example: 301,125,489

I can't figure out the syntax to do so...when I update the cookie the values are stored like this: 301%2C125%2C489

I thought that by switching out the .','. for the ISO-8859-1-or- utf-8 equivalent that it would change the values stored in the cookie but, it only made it worse.

Any thoughts?

if ($_GET['action']=="add" && ($ID = intval($_GET['id'])) > 0) {
            $current_value = $_COOKIE['mypictures'];
            if($current_value != $ID){
                  $cookie_value = $current_value.','.$ID;
                  $cookie_name       = "mypictures";
                  $cookie_life      = time() + 6*60*60; // SIX HOURS - CHECK THIS VALUE CAREFULLY
                  setcookie($cookie_name, $cookie_value, $cookie_expires,'/');

Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 11 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 11 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros