Page 1 of 1

It can't find cookies...? And how do I unset() them?

Posted: Fri Sep 23, 2005 2:44 pm
by Skara
Dunno what I'm doing wrong. I've got a couple of cookies set:

Code: Select all

setcookie('SKP_USER',$_POST['user'],time()+60*60*24*7);  // 7 days
setcookie('SKP_PASS',md5($_POST['pass']),time()+60*60*24*7);
And when I check for them, it doesn't find them:

Code: Select all

function is_logged() {
  if (isset($_COOKIE['SKP_USER']) && isset($_COOKIE['SKP_PASS'])) {
    $user = $_COOKIE['SKP_USER'];
    $pass = $_COOKIE['SKP_PASS'];
    return true;
  }
  return false;
} // truncated
is_logged() always returns false. :(

Yes, double-checked that the cookies are indeed set.

Edit: Another question... unset($_COOKIE['name']) refuses to work. How do I unset a cookie?? O.o

Posted: Fri Sep 23, 2005 2:57 pm
by Jenk
You need to use

Code: Select all

<?php
function is_logged() {
  if (isset($_COOKIE['SKP_USER']) && isset($_COOKIE['SKP_PASS'])) {
    $user = $_COOKIE['SKP_USER'];
    $pass = $_COOKIE['SKP_PASS'];
    return true;
  }
else {
  return false;
}
} // truncated 
?>

Posted: Fri Sep 23, 2005 3:10 pm
by ryanlwh
you cannot access the cookie immediately after setting it. it would not be available until the next page or you reload the page.

and Jenk, I believe return would end the function already. The else part is not needed.

Posted: Fri Sep 23, 2005 3:39 pm
by feyd
deleting a cookie:

Code: Select all

setcookie('cookieName','',time() - 60*60*24*31);
you need to remember that the additional arguments much match what was set in to original cookie for it to take. i.e. the path, domain and secure arguments must be the same between the cookie settings..

Posted: Fri Sep 23, 2005 3:42 pm
by Skara
Ah, hm. Ok, so the path fixed my first question. I just set it to '/' and it works fine everywhere.

I still can't seem to unset() them, though.

@Jenk: Like ryanlwh said, return ends the function, therefore else is redundant.

Posted: Fri Sep 23, 2005 4:27 pm
by feyd
unset() on a cookie will not destroy the cookie.. it'll simply destroy your copy of the cookie.

The code I listed will delete a cookie.

Posted: Fri Sep 23, 2005 6:02 pm
by Skara
oh, missed that. O.o Wow, must bot ne as tober as I sought. ^_^;