Jump to content

deleting cookies won't work. Need help


Imad

Recommended Posts

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

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();
}
}

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?

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.

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.