Jump to content

Cookies set with a link, but not with a form


php_joe

Recommended Posts

Hi,

 

I'm having a problem with my cookies.

 

Here are my login & logout functions:

function login($id, $password){
setcookie("password", "$password");
setcookie("id", "$id");
header("Location: ?");
}
function logout($id, $password){
setcookie("password", "$password", time() - 1);
setcookie("id", "$id", time() - 1);
header("Location: ?");
}

 

Then the script that calls the functions:

if($action == "login"){
$lowercase_id = strtolower($login_id);
login($lowercase_id, $login_password);
}
if($action == "logout"){
logout($id, $password);
}

 

Then I have a code that checks isset() and displays the appropriate info:

if(isset($cookies)){
echo "<div>You are logged in.<br /><a href=\"?action=logout\">Log Out</a></div>";
}else{
echo "You are logged out.<br /><a href=\"?action=login\">Log In</a>";
//require "./not_logged_in.php";
}

 

The problem is: if I use this link:

echo "You are logged out.<br /><a href=\"?action=login\">Log In</a>";

it works fine, but if I require the logged out page:

require "./not_logged_in.php";

it will run the login() function, but the isset() will fail.

 

not_logged_in.php looks like this:

<form method="post">
<input type="hidden" name="action" value="login" />
Name:<input type="text" name="login_name" />
Password:<input type="password" name="login_password" />
<input type="submit" name="submit" value="Log In" />
</form>

 

if the code runs the login() function and isset() works when I use the link, why does it run the login() function and isset() fails when I use the form to post the info to the page?

 

I'm not even checking the information posted against anything yet, shouldn't the fact that $action == "login" be enough...  ???

sorry, I forgot to put that in.

$cookies2 = $_COOKIE['password'];
$cookies = $_COOKIE['id'];

I made the user ID lowercase to avoid problems in the future with people registering under the same name using upper & lower case letter. it doesn't really matter to the problem at hand.

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.