Jump to content

PHP cookies not working


jlr2k8

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
Link to comment
https://forums.phpfreaks.com/topic/19860-php-cookies-not-working/
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.

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.