Page 1 of 1

deleting cookies

Posted: Sun Jan 25, 2004 1:40 am
by Sevengraff
I am getting very upset. I am using both Mozilla Firebird 0.7 and IE 5.0. On firebird, I can never get rid of a cookie that I made, and on IE it goes away sometimes.

This is the code I use, it is part of a class:

Code: Select all

/**
	 * @return void
	 * @param int
	 * @desc Will log in a user and make a cookie. Pass in for how long (in minutes)
	 * @date 12-19-03
	 */
	function login( $t ) {
		if( $this->isLogedIn ) 
			return;
		$time = time() + ( $t * 60 );
		setcookie('nlb3', $this->id . '::' . $this->dataї'password'], $time);
		$this->isLogedIn = true;
	}
	
	/**
	 * @return VOID
	 * @desc Delete login cookies.
	 * @date 01-09-04
	 */
	function logout() {
		setcookie('nlb3', "", time() - 3600);	// set value to nothing, expire date in past
		$this->isLogedIn = false;
		unset( $_COOKIEї'nlb3'] );
	}
I simply call the code like so:

$user->login( $mins );
$user->logout();

I was thinking that the problem might be that I am running this all through http://localhost, but I started going by my ip address and the problem still exists. I am using print_r( $_COOKIE ) to confirm that the cookie exists after I call logout() and goto different pages.

The php manual on deleting cookies:
When deleting a cookie you should assure that the expiration date is in the past, to trigger the removal mechanism in your browser.
is the problem my browser? Im going crazy because im able to log out of forums and other scripts that use cookies, but not the one I create :evil: :cry:

Posted: Sun Jan 25, 2004 8:45 am
by Gen-ik
Don't forget that any changes to your cookies won't take effect until the browser/page is reloaded. So deleting (or setting) a cookie and then trying to see any changes within the same script won't work.

I can't see anything wrong with the way you are setting them or deleting them though.

On another note don't you need $this->isLogedIn = false; in your logout function? (just wondering).

Posted: Sun Jan 25, 2004 12:22 pm
by Sevengraff
that is the problem, I goto other pages and the cookie continue to exist. I call the above code on login.php, and when i goto index.php where I have print_r( $_COOKIE ), my cookie shows up! Its very annoying.

and I have that in there because other functions look at isLogedIn to decided if the user is loged in. it helps on the page where the cookie is suppost to have been deleted.

Posted: Sun Jan 25, 2004 1:19 pm
by Gen-ik
Try removing unset( $_COOKIE['nlb3'] ); from your logout function.. it's not needed because you are already deleting the cookie by setting it's expire date to time()-3600. That may or may not be the problem but it's worth a try.

Are you sending any output to the browser before setting or deleting cookies? They work in the same way as header() does in that no browser output is allowed before you use setcookie().

Posted: Sun Jan 25, 2004 1:38 pm
by Sevengraff
Yeah, I didn't think unset() would help, but I had to try something.

No output. I just went and checked all my files to make sure there isn't anything outside of <?PHP and ?>. Also, I have error reporting set to all, so it should give a warning or something if that was the case.