PHP - Create cookie with multiple values

Asked

Viewed 358 times

0

Good Afternoon,

I am creating a script that will add cookies to the user account. The problem is... I want to add several cookies for example:

If the user clicks add to the advertisement 1 it will create a cookie with the ID 1. In 2h, if the user clicks on the advertisement 10 he will add 1 more cookie, thus leaving 2 cookies. ID 1 and ID 10.

I created it that way:

    if(isset($_GET['add_favorite'])) {

       $favorite_id = mysqli_real_escape_string($con, sanitize($_GET['add_favorite']));
       setcookie("fav_ads_ids", $favorite_id, time() + (86400 * 30), "/");           
    } 

The problem is that it simply does a cookie UPDATE, and that’s not what I want. I want to create viarias cookies for various ads in favorite with the same name "fav_ads_ids".

1 answer

2

Multipolos cookies with the same index

json_encode() does the transformation of the php array to string, thus being able to be saved in the cookie that has as a requirement a parameter string

json_decode() transforms the array into a format string for the format php

if(isset($_GET['add_favorite'])) {
if(isset($_COKKIE['fav_ads_ids'])){ // verifica se o cokkie exite
    $ads = implode(',', json_decode($_COOKIE['fav_ads_ids'])); // organiza o array existente em: 1,2,3 
    $favorite_id = SELECT id FROM suaTabela WHERE id NOT IN ({$ads}) ORDER BY RAND() LIMIT 1 // faz a busca no sql de forma que remove os valores contidos na variavel $ads e ordana o resultado dinamico, selecionando apenas um para exibir
    $arr = json_decode($_COOKIE['fav_ads_ids']); // busca os valores do cokkie
}else{
    $favorite_id = SELECT id FROM suaTabela ORDER BY RAND() LIMIT 1 // seleciona todos os campos do sql e ordana o resultado dinamico, selecionando apenas um para exibir
    $arr = []; // novo valor do cokkie
}
$arr[] = $favorite_id['id']; //adiciona o novo item no array
$arr = json_encode($arr); // prepara o array para ser salvo
setcookie("fav_ads_ids", $arr, time() + (86400 * 30), "/"); // salva o array
}
print_r($_COOKIE['fav_ads_ids']); // imprime os valores do array

example

  • now we just need to configure the search in sql to generate the values for the cookie

  • but that’s not quite it, is that I do not want to add soon the ID 1 or 2, will depend on the user. If I come an ad with ID ex: 3, I add in the correct favorites. Hours later I see another one with different ID and I add favorites... understand? The user will not choose 2 ID next. This is a Bookmark System

  • Cookie Content: %7B%221%22%3A%22215%22%2C%222%22%3A%22215%22%7D ?

  • humm blz pera ai that I will redo then, because this and the base... I will edit for you

  • pq and the following if you have nothing ready the first thing you have to do and measure how many ads you have in db, then if it has none of a random?

  • yes this, is exactly isos

  • Warning: Trim() expects Parameter 1 to be string, Object Given in D: xampp htdocs Superfacil_v1.3.7 inc functions.php on line 1038

  • @Ruialvez Eae solved?

  • not...I think I need a tutorial on how to make Favorite System :S difficult

  • @Ruialvez put the page there full I’ll give you a hand there you study her

  • @Ruialvez your way of how to search in mysql

Show 6 more comments

Browser other questions tagged

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