Jump to content

Cookies are not setting on localhost.


Guest convention

Recommended Posts

Guest convention
I have a login script that will set a cookie containing the entered username in the form:

[code]
if ($username && $password && $submit) {
          // SQL queries to validate username/password will be added later.
setcookie("username",$username,time()+36000,false);
}
[/code]

Which, of course, does not work. I think I tried every single thing imaginable to get to work. This does not work on localhost, but I uploaded the same exact page to my webserver and it worked perfectly. I even added '<?php setcookie
("test","asdfg"); ?>' at the very beginning of the page, and it did not work.

Please, please help me fix this problem! Thank you very much.

[size=8pt]PS: All $_POST variables are declared earlier in the script.[/size]
Link to comment
https://forums.phpfreaks.com/topic/24301-cookies-are-not-setting-on-localhost/
Share on other sites

Guest convention
[quote author=Daniel0 link=topic=111865.msg453627#msg453627 date=1161159962]
Odd. Try this: [code]error_reporting(E_ALL);
setcookie("test","this is a test);
print_r($_COOKIE);[/code] and tell me what you get.
[/quote]

All that is output is 'Array ( )'.
Well, then you haven't turned errors on. I meant to type this: [code]error_reporting(E_ALL);
setcookie("test","this is a test");
print_r($_COOKIE);[/code]

But what I posted should give an error like this: [code]Parse error: parse error, unexpected $end in *file* on line *line*[/code]

Set [tt]display_errors[/tt] in php.ini to [tt]On[/tt] to display errors. Then try with the fixed code (you might have to refresh).
[quote author=xsist10 link=topic=111865.msg453647#msg453647 date=1161162066]
To set cookies you need to start a session.

Put this at the top of your code before cookie creation:
[code]
<?php
session_start();
?>
[/code]
[/quote]

I thought this was the case but when I re-checked the documentation I couldn't find anything about it.

Regards
Huggie

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.