0

Okay so I'm pretty confused right now, on why my $_COOKIE is not being set when the path is set to '/', but is set otherwise. As it currently stands this code

setcookie("scauth", $cookie_content,  time()+60*60, "/", $_SERVER["HTTP_HOST"]);

returns with the error Undefined index: scauth in C:\xampp\htdocs\pnp_site\media\zoo\elements\supercontact\send.php on line 19, but if I was remove the two parameters for path and domain, and run this code for instance..

setcookie("scauth", $cookie_content,  time()+60*60);

The cookie would be instantiated as intended. Which makes this confusing because my understanding of the $path parameter according to setcookie() documentation, explicitly states anything under the set path of '/' will be available within the entire domain (but for some reason it isn't).

The path on the server in which the cookie will be available on. If set to '/', the cookie will be available within the entire domain.

Any ideas on why this is happening? I'm running Windows 7 ApacheFriends XAMPP Version 1.8 test environment.

classicjonesynz
  • 4,012
  • 5
  • 38
  • 78
  • I guess line `16` is that `setcookie`? And you said you are trying to set a path `\` I guess you wanted to write `/`, right? –  Jun 09 '13 at 00:56
  • all `line 19` is on `send.php` is `print_r($_COOKIE['scauth'])`. – classicjonesynz Jun 09 '13 at 00:59
  • have you tried `setcookie("scauth", $cookie_content, time()+60*60, "/");`? –  Jun 09 '13 at 01:02
  • 1
    Are you trying to access the cookie immediately after setting it? If so, your problem might not be related to the path, and more likely is related to this: http://stackoverflow.com/questions/3230133/accessing-cookie-immediately-after-setcookie – FThompson Jun 09 '13 at 01:02
  • also make sure your `php.ini` file allows cookies –  Jun 09 '13 at 01:04
  • possible duplicate of [why cant i create cookies in Firefox?](http://stackoverflow.com/questions/1877984/why-cant-i-create-cookies-in-firefox) – FThompson Jun 09 '13 at 01:08
  • @Gerep yeah `setcookie("scauth", $cookie_content, time()+60*60, "/");` sets the cookie as intended, is it a `localhost` thing? or a `browser` thing? – classicjonesynz Jun 09 '13 at 01:11
  • A domain of localhost:81 is probably not acceptable, so I suspect the domain argument cannot be set when testing locally. – Andy G Jun 09 '13 at 01:26

1 Answers1

0

Try replacing $_SERVER["HTTP_HOST"] with a quoted string version. This will tell you if the format of this information is correct or not.

Use print_r($_SERVER["HTTP_HOST"]) as well, and view the source of the page to read it more easily.

It is not usually necessary to supply the domain.

If testing this locally some browsers will restrict local file-access, and HTTP_HOST will probably not be in the correct format.

Andy G
  • 19,232
  • 5
  • 47
  • 69
  • `$_SERVER["HTTP_HOST"]` is `localhost:81`. – classicjonesynz Jun 09 '13 at 01:02
  • Some browsers will not allow "local file access". At least, not without changing a setting for the browser. Also, do you really need to set the domain? You can just set the path as '/'. – Andy G Jun 09 '13 at 01:04
  • Hmm good point, what are the security benefits of setting the domain? I'd presume to stop cookie hijacking? (also FYI, I haven't -1 rep, on your question but you may wish to edit it to provide more details on how you're answering it) Also I removed the domain parameter and it seems to set the cookie as intended. +1 rep for the suggestion. – classicjonesynz Jun 09 '13 at 01:06
  • I did not downvote this answer, but it seems more of a comment than an answer. – FThompson Jun 09 '13 at 01:09
  • Yes, you are right, thank you. I'll exercise more care in future. – Andy G Jun 09 '13 at 01:10
  • Setting the domain species what domain, and higher subdomains, the cookie will be available in. I do not know if this offers any additional security benefits. – Andy G Jun 09 '13 at 01:21