Jump to content

Multiple Cookies not working


brooksh

Recommended Posts

That code works for me but, as Jessica stated, it won't be what you think it should be. To make the subkey of the array be the value of $id you could either use double quites when defining the cookie or concatenate the variable

setcookie("mycookie[$id]", $id, time()+3600*24);
// . . . OR
setcookie('mycookie['.$id.']', $id, time()+3600*24);

And then you need to remove the variable from inside the single quotes when referencing it as well

echo "Saved value: " . $_COOKIE['mycookie'][$id];

But, again, the code you have will work, but the key would just be the string '$id' (i.e. dollar sign, letter 'i', letter 'd'). One reason you may not bee seeing the value is if you are trying to reference the value on the same page load as when you set it. Per the manual: http://php.net/manual/en/function.setcookie.php

 

 

 

Once the cookies have been set, they can be accessed on the next page load . . .

 

EDIT: Also, not sure why you are setting the array key and the value using the same variable. Seems like a waste to me.

Edited by Psycho
Link to comment
Share on other sites

Is there a better way? All I need is when a visitor visits a certain page and clicks on agree, it remembers it in a cookie and they won't have to click on the link again. But I want them to click agree on multiple pages.

 

page1.php (agree yes then display page)

page2.php (not agree, don't display page)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.