9

On one page I have something like this

setcookie('user', 'value' ,6000, '/', 'mydomain.co.uk');

On the subsequent page I have

var_dump($_COOKIE);

I can see all the automatically generated ones, like PHPSESSID but I cannot see user.

If I do echo setcookie('user', 'value' ,6000, '/', 'mydomain.co.uk'); it returns true. So I'm not sure why I can't see it.

I have tried a lot of different ideas, but nothing has worked. Also, I have using .htaccess to redirect all requests via one page index.php not sure if this is doing anything.

Alex
  • 5,364
  • 9
  • 54
  • 69
  • You might find [`new Cookie($name)`](https://github.com/delight-im/PHP-Cookie/blob/004cde69ec840e65c15275e09b92ecb1da06f357/src/Cookie.php#L51) helpful, as found in [this standalone library](https://github.com/delight-im/PHP-Cookie). – caw Sep 21 '16 at 02:39

2 Answers2

19

Try this:

setcookie('user', 'value' ,time() + 6000, '/', 'mydomain.co.uk');

The expires Parameter needs to be a timestamp. 6000 as a timestamp is in the past and therefore removes the cookie.

js-coder
  • 8,134
  • 9
  • 42
  • 59
  • 1
    Added note: the expires parameter can be set to 0 which basically makes the cookie expire at the end of the session. http://php.net/manual/en/function.setcookie.php – diggersworld Nov 14 '12 at 16:56
3

How about:

setcookie('user', 'value' ,6000, '/', '.mydomain.co.uk');

Check your brower's cookies. Some browsers (firefox and chrome) have addons that allow you to see cookies as they come in so you can debug.

EDIT: The problem is 6000. That is wrong. use this: time() + 6000

roychri
  • 2,866
  • 1
  • 21
  • 29
  • Yeah I've been using a Chrome add-on but resorted to var_dumping $_COOKIE when it wasn't appearing in there either. I've tried using '.mydomain.co.uk' but it's still not working :( – Alex Mar 13 '12 at 19:16