Jump to content

Archived

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

Guest convention

Cookies are not setting on localhost.

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]

Share this post


Link to post
Share on other sites
Try enabling errors and putting error_reporting(E_ALL); on top of your page to see if it returns any errors.

Share this post


Link to post
Share on other sites
Guest convention
[quote author=Daniel0 link=topic=111865.msg453605#msg453605 date=1161156361]
Try enabling errors and putting error_reporting(E_ALL); on top of your page to see if it returns any errors.
[/quote]

Hmm... not getting any errors. :\

Share this post


Link to post
Share on other sites
Odd. Try this: [code]error_reporting(E_ALL);
setcookie("test","this is a test);
print_r($_COOKIE);[/code] and tell me what you get.

Share this post


Link to post
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 ( )'.

Share this post


Link to post
Share on other sites
Try this then...

[code]
<?php
error_reporting(E_ALL);
setcookie("test","this is a test");
foreach ($_COOKIE as $k => $v){
  echo "$k - $v<br>\n";
}
?>
[/code]

Regards
Huggie

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
[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

Share this post


Link to post
Share on other sites
To set sessions session_start is required (or session.auto_start must be on in php.ini).

It has nothing to do with cookies.

Share this post


Link to post
Share on other sites
I'm pretty sure that you need a session ID to be able to create cookies. And you can only get that with session_start.

[me=xsist10]off to do some Googling.[/me]

Share this post


Link to post
Share on other sites
Nope. Check [url=http://php.net/setcookie]the manual's examples[/url]. And my example do work on my system, and it is without session_start.

Share this post


Link to post
Share on other sites

×

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.