Jump to content


Cookies are not setting on localhost.


  • Please log in to reply
11 replies to this topic

#1 Guest_convention_*

Guest_convention_*
  • Guests

Posted 18 October 2006 - 07:07 AM

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

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

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.

PS: All $_POST variables are declared earlier in the script.

#2 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 18 October 2006 - 07:26 AM

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

#3 Guest_convention_*

Guest_convention_*
  • Guests

Posted 18 October 2006 - 07:36 AM

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


Hmm... not getting any errors. :\

#4 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 18 October 2006 - 08:26 AM

Odd. Try this:
error_reporting(E_ALL);
setcookie("test","this is a test);
print_r($_COOKIE);
and tell me what you get.

#5 Guest_convention_*

Guest_convention_*
  • Guests

Posted 18 October 2006 - 08:29 AM

Odd. Try this:

error_reporting(E_ALL);
setcookie("test","this is a test);
print_r($_COOKIE);
and tell me what you get.


All that is output is 'Array ( )'.

#6 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 18 October 2006 - 08:42 AM

Try this then...

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

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#7 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 18 October 2006 - 08:44 AM

Well, then you haven't turned errors on. I meant to type this:
error_reporting(E_ALL);
setcookie("test","this is a test");
print_r($_COOKIE);

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

Set display_errors in php.ini to On to display errors. Then try with the fixed code (you might have to refresh).

#8 xsist10

xsist10
  • Members
  • PipPipPip
  • Advanced Member
  • 114 posts

Posted 18 October 2006 - 09:01 AM

To set cookies you need to start a session.

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

SA PHP Archives - www.phparchives.za.org

#9 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 18 October 2006 - 09:15 AM

To set cookies you need to start a session.

Put this at the top of your code before cookie creation:

<?php
session_start();
?>


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

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#10 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 18 October 2006 - 09:20 AM

To set sessions session_start is required (or session.auto_start must be on in php.ini).

It has nothing to do with cookies.

#11 xsist10

xsist10
  • Members
  • PipPipPip
  • Advanced Member
  • 114 posts

Posted 18 October 2006 - 09:49 AM

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.

*xsist10 off to do some Googling.
SA PHP Archives - www.phparchives.za.org

#12 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 18 October 2006 - 10:13 AM

Nope. Check the manual's examples. And my example do work on my system, and it is without session_start.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users