Add and remove favorite item with Cookie

Asked

Viewed 167 times

1

I have the code to save the cookie catching the id of postage

$id= "5100";
setcookie("itemfavorito", $id, time()+3600*24*30*12*1);

But I wanted a help that’s how to do for when the person goes on another page with another id he save together by staying like that in the cookie barring 5100/5101/5102 and so it goes...

I know it must be with explode/implode, but I don’t know how to apply.

And also when removing the cookie page, how would it be? To remove the right page value it would be for example 5101

  • Curiosity: what will be the use of this cookie?

  • To add the page that the person added to the bookmarks, taking the page ID... like this site thenightfilmes.net is cookie-like, but I’m not getting one based on it

1 answer

1


To "add" a value to a cookie, all you need to do is read the current value that was sent to you with the current request, add the new data and set the result as a cookie with the same key.

On each page just change the value of the variable $id and include the page setar_cookie.php

Pages

$id= "5100";
include("setar_cookie.php");

setar_cookie.php

if(!isset($_COOKIE["itemfavorito"])) {
    setcookie("itemfavorito", $id, time()+3600*24*30*12*1, "/");
} else if (strpos($_COOKIE["itemfavorito"],$id)===false){
    $addId= $_COOKIE["itemfavorito"]."/".$id;
    setcookie("itemfavorito", $addId, time()+3600*24*30*12*1, "/");
}

To remove the right page value

$cookie= $_COOKIE['itemfavorito'];
//exemplo remover 5101

$id= join('/',array_diff(explode('/', $cookie), array('5101')));

setcookie("itemfavorito", $id, time()+3600*24*30*12*1, "/"); 

in the ideone

OBS: To / (bar) in the setcookie serves to indicate that it works for the entire site and not just the directory where it is being configured setcookie


If you want to keep the cookie always ordered

if(!isset($_COOKIE["itemfavorito"])) {
    setcookie("itemfavorito", $id, time()+3600*24*30*12*1, "/");
} else if (strpos($_COOKIE["itemfavorito"],$id)===false){
        $addId= $_COOKIE["itemfavorito"]."/".$id;
        $array= explode("/",$addId);
        sort($array);
        $joinId= join('/',$array); 
        setcookie("itemfavorito", $joinId, time()+3600*24*30*12*1, "/");
}

And if you have an "5101" ID added and you add another "510"

In that case we can use:

setar_cookie.php

$tags = $_COOKIE["itemfavorito"];
$tagsArray = explode('/', $tags);

if(!isset($_COOKIE["itemfavorito"])) {
    setcookie("itemfavorito", $id, time()+3600*24*30*12*1, "/");
} else if (!in_array($id, $tagsArray)) {
        $addId= $_COOKIE["itemfavorito"]."/".$id;
        $array= explode("/",$addId);
        sort($array);
        $joinId= join('/',$array); 
        setcookie("itemfavorito", $joinId, time()+3600*24*30*12*1, "/");
}
  • I tried it here but it got a bit bumpy, I access a page there adds the cookie with start ; then adds ;/5101 I’m trying to do it based on this site thenightfilmes.net it uses exactly as I’m trying, but it’s kind of complicated to do

  • @Rogériosilva, to unpack, you have to post the code you made

  • I added the way you put it there in the example, the first code on the page as soon as the person clicks it adds to the bookmarks, but adds "/" and ";" to the cookie too, the example I want is on this site thenightfilmes.net he did this function, but I can’t make one based on it

  • It worked, can you help me with something else? if the person enters a page with the ID already saved in the cookie, show a message ex: SAVED IN FAVORITES

  • Yes, but in what way, a warning, a modal? Or simply an echo?

  • A normal echo, with a warning that was saved in the bookmarks the visited page

  • @Rogériosilva, both in the ifhow much in the else place $favoritado = "Esta página foi salva nos favoritos"; Then just give one echo $favoritado; at the place where the warning is to be displayed.

  • This way it works, it shows the message that was saved, but what I wanted was when entering the page with such ID checks if you have in the cookie this value saved there shows the message FAVORITE PAGE or NOT FAVORITE

  • @Rogériosilva, there changes the focus of the question that is como fazer para quando a pessoa for em outra página com outro id ele salvar junto ficando assim lá no cookie salvo 5100/5101/5102 e assim vai.... In order to show FAVORITE or NOT FAVORITE PAGE you should have on the page a button for the user to click and save the cookie so that the page is part of the favorites. While it does not click the button will appear the NOT FAVORITE message. In this case better ask another question reporting this situation so that this post is not confused for other users.

  • That was exactly it... The button is already working normal, but as I do for when the page is already favorite, the button change to Not Favorite

  • 1

    In setar_cookie.php puts this line of code if (strpos($_COOKIE["itemfavorito"],$id)!==false){$desabilitar = "disabled";} and the button <button type="button" .... <?php echo $desabilitar; ?>>.....</button>

  • It worked, thank you very much. But what I realized is that when adding the ID to the cookie, if you have a "5101" ID added and you add another "510" it won’t take

  • 1

    @Rogériosilva, edited reply

Show 8 more comments

Browser other questions tagged

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