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...  ???

Link to comment
Share on other sites

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.

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.