1

I'm running a vagrant server on my machine to test out the php side of my website.

I'm trying to use a cookie to remember an option on the site, however it is refusing to transfer to the rest of the site. I've tried specifying the local domain (test.dev), the local IP address... nothing. On the page which is called after a button click, the cookie is set:

Cookie set page:

array(3) {
  ["PHPSESSID"]=>
  string(26) "p6rpk6ct7c9clg5fdr7j8cg4v6"
  ["ukshow"]=>
  string(5) "hello"
  ["__ar_v4"]=>
  string(104) "H3ELFFCLHNBXZJDXN2KICN:20140219:54|37K3GO4OMFDVHJVEKXZ7HL:20140219:54|5YST4SB55RB5LCGWF5RBRL:20140219:54"
}

Index, or any other page:

array(3) {
  ["PHPSESSID"]=>
  string(26) "p6rpk6ct7c9clg5fdr7j8cg4v6"
  ["ukshow"]=>
  string(0) ""
  ["__ar_v4"]=>
  string(104) "H3ELFFCLHNBXZJDXN2KICN:20140219:55|37K3GO4OMFDVHJVEKXZ7HL:20140219:55|5YST4SB55RB5LCGWF5RBRL:20140219:55"
}

I've tried all sorts of setcookie stings, from setting the domain/ip, adding a dot before it, setting the path to /, trying quotes or single quotes... the lot. Nothing works!

An example:

setcookie("ukshow","hello",time() + (86400 * 365),"/",".test.dev");

Edit: forgot to add, I can see them fine in the developer consoles in Safari & Chrome (not checked anything else..)

Coffeee
  • 143
  • 1
  • 2
  • 13

3 Answers3

2

Have you tried setting a cookie for the global domain?

setcookie("ukshow", "hello", time() + (86400 * 365), "/");

or

$folder = '/myfolder/';
setcookie("ukshow", "hello", time() + (86400 * 365), $folder);

This seems to work for me in a very similar environment.

DianaLog
  • 326
  • 2
  • 9
  • Same as before, works on the original script page, but nothing else. Its not in a folder, its all at root ("/") e.g. /index.html /cookieset.php etc – Coffeee Feb 19 '14 at 20:05
1

Fixed. Remember not to use = to find a value! I was setting the value again on the page - stupid! Remember folks: The 3 different equals

Community
  • 1
  • 1
Coffeee
  • 143
  • 1
  • 2
  • 13
0

Make sure you set cookie_secure to false on your local environment.

HappyCoder
  • 5,985
  • 6
  • 42
  • 73