php_joe Posted March 14, 2007 Share Posted March 14, 2007 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... ??? Quote Link to comment https://forums.phpfreaks.com/topic/42724-cookies-set-with-a-link-but-not-with-a-form/ Share on other sites More sharing options...
per1os Posted March 14, 2007 Share Posted March 14, 2007 Your not setting a path or domain. That needs to be done, see php.net and setcookie for correct usage. --FrosT Quote Link to comment https://forums.phpfreaks.com/topic/42724-cookies-set-with-a-link-but-not-with-a-form/#findComment-207278 Share on other sites More sharing options...
php_joe Posted March 14, 2007 Author Share Posted March 14, 2007 Ok, I'll try that. But why does it work when I use the link then? Quote Link to comment https://forums.phpfreaks.com/topic/42724-cookies-set-with-a-link-but-not-with-a-form/#findComment-207285 Share on other sites More sharing options...
Lumio Posted March 14, 2007 Share Posted March 14, 2007 where do you define $cookies? And is the $id a string? If not, why are you trying to lowercase it Quote Link to comment https://forums.phpfreaks.com/topic/42724-cookies-set-with-a-link-but-not-with-a-form/#findComment-207289 Share on other sites More sharing options...
php_joe Posted March 14, 2007 Author Share Posted March 14, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/42724-cookies-set-with-a-link-but-not-with-a-form/#findComment-207299 Share on other sites More sharing options...
php_joe Posted March 14, 2007 Author Share Posted March 14, 2007 well, I don't know what's up, but suddenly it's working fine. I didn't change anything... ??? weird! Quote Link to comment https://forums.phpfreaks.com/topic/42724-cookies-set-with-a-link-but-not-with-a-form/#findComment-207308 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.