Multiple Value with Comma Separation

0

Next, I'm using setcookie in order to get ids of all pages visited per user and thus make a kind of history, however I run into a problem when not being able to update the value of the cookie by adding new page ids in it name.

Whenever the value, "3", changes the cookie changes to the next value, would it have 3 to continue and the new value appears after a comma?

<?php 
setcookie("id", "3");
print_r($_COOKIE); 
?>
    
asked by anonymous 17.02.2018 / 00:56

1 answer

0

You just concatenate the values.

$id = $_COOKIE["id"];
$novoId = "Novo-Valor";

if (!preg_match("/\b{$novoId}\b/", $id)) {
    setcookie("id", $id .= ",{$novoId}");
}

And to separate:

var_dump( explode(",", $_COOKIE["id"]) );
    
17.02.2018 / 01:09