Reputation:
I have a classifieds site, and on the main page I want the last visited ads by the existing user to show up.
How would I do this?
Basically, it has to be something like this:
Is it even possible to add values to an existing cookie ?
Upvotes: 0
Views: 11628
Reputation: 265261
you can use the string concatenate operator:
setcookie('ad_ids', $_COOKIE['ad_ids'] . ';'.$new_id);
Upvotes: 3
Reputation: 3081
Use an array of viewed classifieds:
$arr = array('1', '2', '3');
setcookie('viewedads', serialize($arr), time()+10000, '/');
then if you want to add more ads:
$arr = unserialize($_COOKIE['viewedads']);
//new add
$arr[] = '4';
setcookie('viewedads', serialize($arr), time()+10000, '/');
Upvotes: 1
Reputation: 346317
Cookies basically work like this: to set a cookie, the server sends its name and value to the client with a HTTP header in any HTTP response. After that, the client will send that key and value as a HTTP header with every request to that server.
So in order to "add" a value to a cookie, all you have to do is read the current value which was sent to you with the current request, add the new data, and set the result as a cookie with the same key in your response.
Upvotes: 5