deleting cookies

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
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

deleting cookies

Post 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:
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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).
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post 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.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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().
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post 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.
Post Reply