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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

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

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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 
?>
ryanlwh
Forum Commoner
Posts: 84
Joined: Wed Sep 14, 2005 1:29 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

oh, missed that. O.o Wow, must bot ne as tober as I sought. ^_^;
Post Reply