Multiple value with comma separation

Asked

Viewed 27 times

0

Next, I am using setcookie in order to get ids of all pages visited per user and so do a kind of history, but I come across a problem when not the possibility to update the cookie value by adding new page id in the same name.

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

<?php 
setcookie("id", "3");
print_r($_COOKIE); 
?>

1 answer

0


All you have to do is 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"]) );
  • he is duplicating the id

  • @Gabriel updated my reply. I added a check to register only new values.

  • I just have to thank you, man, thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.