Ashley Davies
Ashley Davies

Reputation: 1893

PHP: Cookie not unsetting

<?
if($_POST["Login"])
{
    if (GetRightPassword($_POST["emaillogin"],$_POST["passwordlogin"]))
    {
        $_SESSION["email"] = $_POST["emaillogin"];
        $_SESSION["password"] = $_POST["passwordlogin"];
        echo "Keeping logged in: ".$_POST["keeploggedin"];
        if ($_POST["keeploggedin"])
        {
            setcookie("email", $_POST["emaillogin"], time()+60*60*24*365); 
            setcookie("password", $_POST["passwordlogin"], time()+60*60*24*365);
        }
    }
    else
    {
        echo "Invalid username/password!";
    }
}

if($_POST["Logout"])
{
    $_SESSION["email"] = null;
    $_SESSION["password"] = null;
    setcookie("email", "", time()-900000); 
    setcookie("password", "", time()-900000);
}

echo $_COOKIE["email"];
?>

That's the only code (As far as I can find, I coded it 6 months ago minimum but I'm prety sure there's no more) that writes to cookies or session.

When I click logout, it nulls the session variables, so when the page loads, I'm logged off- Change page again or refresh though, and I'm logged back in again.

Any ideas why? Login isn't being sent when I change page, so I have no idea why.

If it helps, the echo $_COOKIE["email"]; line echos your email even when it's just set it to "".

Edit

I just found more code related to this.

This code is ran before that code.

if(isset($_COOKIE["email"]))
{
$_SESSION["email"] = $_COOKIE["email"];
$_SESSION["password"] = $_COOKIE["password"];
}

Upvotes: 0

Views: 370

Answers (1)

Rene Pot
Rene Pot

Reputation: 24815

set in all your cookies the path and domain. This ensures it's not something like a with and withoud www.

Check the documentation how to do this: http://nl2.php.net/setcookie

This might be the root cause for your problem

Upvotes: 1

Related Questions