Search code examples
phpcookiessetcookie

Using cookies with php


I'm just trying to set and use a cookie but I can't seem to store anything.

On login, I use:

setcookie("username", $user);

But, when I use Firefox and the Web Developer plugin Cookies -> View Cookie Information There is no username cookie.

Also, when I try to access the value from a subsequent page using

$_COOKIE["username"]

It is returning null/empty

var_dump(setcookie("username", $user)); RESULT: bool(true)

and

var_dump($_COOKIE) RESULT: specific cookie does not exist (others are there)

I have done some more testing...

The cookie exists after login (first page) but disappears when I go to another (2nd page) and is lost for good...

Are there any headers that must be present or not present?


Solution

  • Thanks everyone for the feedback... Aditya lead me to further analyse the cookie and I discovered that the path was the issue...

    The login path was /admin/ and then I was redirecting back to the root...

    Thanks all for your help and feedback!