Jump to content

Archived

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

jlr2k8

PHP cookies not working

Recommended Posts

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:

[code]
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
}
[/code]

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


I use PHP 5 which runs on IIS 6.0.

Thanks for your help,
Josh Rogers

Share this post


Link to post
Share on other sites
It might be a path issue.  Try:

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

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.

Share this post


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

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.