Jump to content


Photo

PHP cookies not working


  • Please log in to reply
2 replies to this topic

#1 jlr2k8

jlr2k8
  • Members
  • PipPip
  • Member
  • 20 posts
  • LocationIssaquah, WA

Posted 06 September 2006 - 05:08 AM

Ok, so I'm making (or I guess... attempting to make :D) this login system using PHP. The login script, assuming the user is successfully authenticated, will generate a cookie with the username and a cookie with the password.

Here is part of the validation script:

if($_POST['password'] != "$password")
{
	header("cache-control: no-cache, must-revalidate");
	header("location: ../user.php?content=l&s=f"); //redirect to login page, with a failure notification
}
else
{
	$userName = $_POST["userName"];
	$password = $_POST["password"];
	
	setcookie("WFS2U", $userName);
	setcookie("WFS2P", $password);
	
	header("cache-control: no-cache, must-revalidate");
	header("location: ../index.php"); //redirect to home
}

Then to display the cookies, I use:
echo $_COOKIE["WFS2U"];


I use PHP 5 which runs on IIS 6.0.

Thanks for your help,
Josh Rogers

#2 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 06 September 2006 - 05:23 AM

It might be a path issue.  Try:

setcookie("WFS2U", $userName, time()+60*60*24, '/')

That time()+60*60*24 sets it to expire in 1 day.  You have to include an expiry time in order to set the cookie path (yes, it doesn't make sense, it's because of the argument order).

Also, remember that $_COOKIE[] only contains cookies received from the browser.  You will only see these in the NEXT request, and not in the same request that you set the cookie.

#3 jlr2k8

jlr2k8
  • Members
  • PipPip
  • Member
  • 20 posts
  • LocationIssaquah, WA

Posted 06 September 2006 - 01:51 PM

Hmm I noticed something though. The cookie only sets if I don't have header() on the same page. Why is that?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users