im having troubles with setting cookies in my website (since its the first time using them). Basicly i have this form on a article page:

<form method="POST" action="">
    <input type="submit" value="<?php echo $id ?>"  name="commentData">

and with this function i can store the id in my cookie:

if (isset($_POST['commentData'])) {
        $cookie_value = $_POST["commentData"];
        $cookie_name = "saved_articles";
        setcookie($cookie_name, $cookie_value, time() + 600, '/');

Now i need to be able to store multiple ids in my cookies so i can loop on my 'saved articles' page. Currently it always override the value. Any help would be greatly appreciated

  • So you want to get the existing cookie value, append the new value to that to get a list of IDs and then set the list as the new cookie value? If these are just numeric values you could just use a list of integers as a comma-separated string. If it's something more complicated then you can store them in a list and serialize the value before storing it in a cookie, and unserialize the value you get back (but make sure to sanity-check the types and values that are returned before using them, as the user can modify their cookies!) – Rup Aug 28 '19 at 11:42
  • Ok, i added them as one string and used expolde() function. Working now – Dennis Aug 29 '19 at 8:53

Browse other questions tagged or ask your own question.