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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.