0

Its not echoing the COOKIE instead shows an error.

Error:Undefined index: pref[fontSize]

setcookie('pref[fontSize]',25);
setcookie('pref[favCategory]','news');
setcookie('pref[screenWidth]',1024);

echo $_COOKIE['pref[fontSize]'];
6
  • What happens when you var_dump($_COOKIE); ? Commented Apr 30, 2015 at 7:53
  • 2
    it will be available on the next request Commented Apr 30, 2015 at 7:54
  • @BeatAlex array(2) { ["pref"]=> array(3) { ["fontSize"]=> string(2) "25" ["favCategory"]=> string(4) "news" ["screenWidth"]=> string(2) "25" } ["PHPSESSID"]=> string(26) "5mr3d4tt3uab6kdkal0blt1qt2" } Commented Apr 30, 2015 at 7:58
  • OK so it is setting then! Like the others have said you need to wait until the next request :) Commented Apr 30, 2015 at 7:59
  • @Ghost what do you mean by next request. Commented Apr 30, 2015 at 7:59

2 Answers 2

1

The documentation for setCookie, suggests that $_COOKIE will not hold vals from setCookie, till after the cookie has been set client side (e.g. next request, because the user might have their browser configured to not save cookies).

Sign up to request clarification or add additional context in comments.

Comments

0

you should go with this

echo $_COOKIE['pref']['fontSize'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.