Cookie troubles

Support, Snippets & Projects

Moderators: Moderator, Global Moderator

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

Cookie troubles

Post by Trinity »

To set the cookie:
[code]<?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";
}
?>[/code]

To unset the cookie:
[code]<?PHP
setcookie("ccAccount", null, time()-1, "/", "", "");
unset($_COOKIE["ccAccount"]);
echo "<meta http-equiv=\"refresh\" content=\"0;URL='http://codecrypt.org/index.php'\" />";
?>[/code]

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]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]

[code]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;
 }
 }[/code]
Trinity
Hero Member
Hero Member
Posts: 808
Joined: Wed May 05, 2004 9:19 am

Cookie troubles

Post by Trinity »

changed the logout code a bit with ner0's help, still no luck:

[code]<?PHP
setcookie ("ccAccount", "", time() - 3600);
echo "<meta http-equiv=\"refresh\" content=\"0;URL='http://codecrypt.org/index.php'\" />";
?>[/code]

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 1:08 pm

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]setcookie("ccAccount", $uInfo, time()+31536000, "/", ".codecrypt.org", "0");[/code] you have to use [code]setcookie("ccAccount", "", time()-1, "/", ".codecrypt.org", "0");[/code] to unset the cookie.
Trinity
Hero Member
Hero Member
Posts: 808
Joined: Wed May 05, 2004 9:19 am

Cookie troubles

Post by Trinity »

Tyvm scott <img src=\'http://www.killanet.net/forum3/public/s ... /smile.gif\' class=\'bbc_emoticon\' alt=\':)\' />
Post Reply

Return to “PHP &amp; MySQL”