Imad Posted July 23, 2008 Share Posted July 23, 2008 Hi guys, I have a login system that I setup. Of course, with a login system, I'd need the logout feature. During login the cookie is set and all works. However, when logging out, it doesn't seem to delete them. Here's the code I have for logging out: elseif ($action == 'logout') { session_start(); session_unset(); session_destroy(); include 'inc/config.php'; if(isset($_COOKIE['forum'])) { setcookie("forum[loggedin]", time() - 3600); setcookie("forum[valid_user]", time() - 3600); setcookie("forum[username]", time() - 3600); setcookie("forum[password]", time() - 3600); } header('Location: login'); exit(); } Any help would be appreciated. Best Regards. Link to comment https://forums.phpfreaks.com/topic/116272-deleting-cookies-wont-work-need-help/ Share on other sites More sharing options...
DarkWater Posted July 23, 2008 Share Posted July 23, 2008 Wait umm....why do you use array syntax for cookie names? Show me how you're setting them. Link to comment https://forums.phpfreaks.com/topic/116272-deleting-cookies-wont-work-need-help/#findComment-597849 Share on other sites More sharing options...
Imad Posted July 23, 2008 Author Share Posted July 23, 2008 Thanks for your reply. I use the array syntax for better use. When you need to check if all the cookies are set, you can do so like this: if(isset($_COOKIE['forum'])) { echo "blah"; } I set them like this: if ($action == 'login') { include 'inc/config.php'; $username = SafeAddSlashes($_POST['username']); $password = SafeAddSlashes(md5($_POST['password'])); $time = time(); $check = SafeAddSlashes($_POST['setcookie']); $query = "SELECT username, password FROM users WHERE username = '$username' AND password = '$password'"; $result = mysql_query($query, $db); if(mysql_num_rows($result)) { setcookie("forum[loggedin]", 1, $time + 3600); setcookie("forum[valid_user]", $username, $time + 3600); if($check) { setcookie("forum[username]", $username, $time + 3600); setcookie("forum[password]", $password, $time + 3600); } header('Location: home'); exit(); } else { header('Location: ?action=logform&inv=1'); exit(); } } Link to comment https://forums.phpfreaks.com/topic/116272-deleting-cookies-wont-work-need-help/#findComment-597859 Share on other sites More sharing options...
DarkWater Posted July 23, 2008 Share Posted July 23, 2008 You never actually set a cookie named 'forum'. Link to comment https://forums.phpfreaks.com/topic/116272-deleting-cookies-wont-work-need-help/#findComment-597862 Share on other sites More sharing options...
Imad Posted July 23, 2008 Author Share Posted July 23, 2008 You never actually set a cookie named 'forum'. I'm almost positive I don't need to. Nevertheless, I did set a cookie named forum and it still won't delete the cookies. Any other ideas? Best Regards. Link to comment https://forums.phpfreaks.com/topic/116272-deleting-cookies-wont-work-need-help/#findComment-597876 Share on other sites More sharing options...
DarkWater Posted July 23, 2008 Share Posted July 23, 2008 I'm pretty sure that you DO need to because of this line: if(isset($_COOKIE['forum'])) { EDIT: Apparently you can use array syntax for cookies and PHP treats them like an array. Hmm. Then again I hardly use cookies so I wouldn't know. >_> Also, why do you use just "login" as the location for your header() call? Link to comment https://forums.phpfreaks.com/topic/116272-deleting-cookies-wont-work-need-help/#findComment-597878 Share on other sites More sharing options...
Imad Posted July 23, 2008 Author Share Posted July 23, 2008 I got the logout to work. I forgot to give the cookies a value during logout. I don't need to set a cookie named forum because when I use this statement: if(isset($_COOKIE['forum'])) It's basically checking if the cookie has the beginning name of forum because I used an array syntax. It makes checking if all the cookies exist a lot easier. Thanks for your help & support. Best Regards. EDIT: I use login as the location in the header because I used an .htaccess trick. Basically a coverup for login.php?action=login. Link to comment https://forums.phpfreaks.com/topic/116272-deleting-cookies-wont-work-need-help/#findComment-597888 Share on other sites More sharing options...
DarkWater Posted July 23, 2008 Share Posted July 23, 2008 Oh. I figured as much (with regards to the header()). Link to comment https://forums.phpfreaks.com/topic/116272-deleting-cookies-wont-work-need-help/#findComment-597892 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.