Cookie troubles

Support, Snippets & Projects

Moderators: Moderator, Global Moderator

Post Reply
Trinity
Hero Member
Hero Member
Posts: 808
Joined: Wed May 05, 2004 2:19 am

Cookie troubles

Post 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;
 	 }
  }
Trinity
Hero Member
Hero Member
Posts: 808
Joined: Wed May 05, 2004 2:19 am

Cookie troubles

Post 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...
Scott
Administrator
Administrator
Posts: 1651
Joined: Sun Apr 25, 2004 6:08 am

Cookie troubles

Post 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.
Trinity
Hero Member
Hero Member
Posts: 808
Joined: Wed May 05, 2004 2:19 am

Cookie troubles

Post by Trinity »

Tyvm scott /smile.gif' class='bbc_emoticon' alt=':)' />
Post Reply

Return to “PHP &amp; MySQL”