Page 1 of 1

Cookie troubles

Posted: Fri Dec 10, 2004 11:56 pm
by Trinity
To set the cookie:

Code: Select all

<?PHP
	function _setCookie($aArgs) {
 	 $uInfo = implode("|", $aArgs);
 	 setcookie("ccAccount", $uInfo, time()+31536000, "/", ".codecrypt.org", "0");
	}
  
	if ($_GET['id'] and $_GET['user'] and $_GET['rank']) {
  $uInfo = array($_GET['id'], $_GET['user'], $_GET['rank']);
  _setCookie($uInfo);
  echo "<meta http-equiv=\"refresh\" content=\"0;URL='http://codecrypt.org/members.php'\" />";
	}
	else {
  echo "You cannot access this file directly";
	}
?>
To unset the cookie:

Code: Select all

<?PHP
	setcookie("ccAccount", null, time()-1, "/", "", "");
	unset($_COOKIE["ccAccount"]);	
	echo "<meta http-equiv=\"refresh\" content=\"0;URL='http://codecrypt.org/index.php'\" />";
?>
It sets the cookie fine, but when I try and log out, it won't.

This is the code to check if its logged in:

Code: Select all

if ($uInfo = $users->getSession()) {
    echo "Thankyou for logging in ". $uInfo['Username'];
    echo "<br /><a href=\"members.php?action=logout\">Click here to log out</a>";
 	 }

Code: Select all

function getSession() {
 	 if (isset($_COOKIE['ccAccount'])) {
    $uInfo = explode("|", $_COOKIE['ccAccount']);
    $return['uID'] = $uInfo['0'];
    $return['Username'] = $uInfo['1'];
    $reutrn['Rank'] = $uInfo['2'];
    return $return;
 	 }
 	 else {
    return false;
 	 }
  }

Cookie troubles

Posted: Sat Dec 11, 2004 6:34 am
by Trinity
changed the logout code a bit with ner0's help, still no luck:

Code: Select all

<?PHP
	setcookie ("ccAccount", "", time() - 3600);	
	echo "<meta http-equiv=\"refresh\" content=\"0;URL='http://codecrypt.org/index.php'\" />";
?>
I should add that the setcookie and getSession parts seem to be working fine, as it can read the Username etc. from the cookie, it just won't unset it...

Cookie troubles

Posted: Sat Dec 11, 2004 8:39 am
by Scott
When unsetting a cookie, you have to use exactly the same parameters as you have when you set the cookie so if you set using

Code: Select all

setcookie("ccAccount", $uInfo, time()+31536000, "/", ".codecrypt.org", "0");
you have to use

Code: Select all

setcookie("ccAccount", "", time()-1, "/", ".codecrypt.org", "0");
to unset the cookie.

Cookie troubles

Posted: Sat Dec 11, 2004 3:30 pm
by Trinity
Tyvm scott /smile.gif' class='bbc_emoticon' alt=':)' />