Solved

Delete all cookies created by the webpage

Posted on 2009-04-15
7
576 Views
Last Modified: 2012-05-06
Hello,

 I'm looking for a code to delete all cookies created by the domain. It can be a javascript or a php code.

 Its better if i can place a link to delete cookies set by the domain.

 i2ve found several codes but they didnt worked for me ike the following one..
FOREACH($_COOKIE AS $key => $value) {
     SETCOOKIE($key,$value,TIME()-10000,"/",".domain.com");
}

Open in new window

0
Comment
Question by:jolly_joker
  • 3
  • 2
7 Comments
 
LVL 3

Expert Comment

by:QualitySoftwareDevelopment
ID: 24147634
$cookiesSet = array_keys($_COOKIE);
for ($x = 0; $x < count($cookiesset); $x++) {br>
   if (is_array($_COOKIE[$cookiesSet[$x]])) {
       $cookiesSetA = array_keys($_COOKIE[$cookiesSet[$x]]);
       for ($c = 0; $c < count($cookiesseta); $c++) {br>
           $aCookie = $cookiesSet[$x].'['.$cookiesSetA[$c].']';
           setcookie($aCookie,"",time()-1);
       }
   }
   setcookie($cookiesSet[$x],"",time()-1);
}
0
 
LVL 3

Accepted Solution

by:
QualitySoftwareDevelopment earned 250 total points
ID: 24147654
Sorry forgot to insert it in code :-)
$cookiesSet = array_keys($_COOKIE);
for ($x = 0; $x < count($cookiesset); $x++) {br>
   if (is_array($_COOKIE[$cookiesSet[$x]])) {
       $cookiesSetA = array_keys($_COOKIE[$cookiesSet[$x]]);
       for ($c = 0; $c < count($cookiesseta); $c++) {br>
           $aCookie = $cookiesSet[$x].'['.$cookiesSetA[$c].']';
           setcookie($aCookie,"",time()-1);
       }
   }
   setcookie($cookiesSet[$x],"",time()-1);
}

Open in new window

0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 24147808
I can see nothing wrong with your original post.  It may be that the cookies appear to still be there because they are ONLY loaded at page load time.  So when you do a setcookie() command the cookies get changed, but they will only be found to be changed on the NEXT page.

Here is what I do in my logout script.  Note the redirect to load a new page.
<?php // RAY_logout.php
error_reporting(E_ALL);
 
 
define('COOKIE_LIFE', 60*60*24); // A 24-HOUR DAY IN SECONDS ( = 86,400 )
$cookie_expires	= time() - date('Z') - COOKIE_LIFE;
 
 
// CLEAR THE INFORMATION FROM THE $_SESSION ARRAY
$_SESSION = array();
 
// IF THE SESSION IS KEPT IN COOKIE, FORCE SESSION COOKIE TO EXPIRE
if (isset($_COOKIE[session_name()]))
{
   setcookie(session_name(), '', $cookie_expires, '/');
}
 
// TELL PHP TO ELIMINATE THE SESSION
session_destroy();
 
 
 
 
 
 
// CLEAR ALL COOKIES WITH THIS CODE
foreach ($_COOKIE as $key => $value)
{
   setcookie($key, '', $cookie_expires, '/');
}
 
 
 
 
 
// REDIRECT TO THE HOME PAGE
header("Location: /");
exit;
 
?>

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 24152006
jolly_joker: Please see the grading guidelines here:

http://www.experts-exchange.com/help.jsp#hi403

You got clear and accurate answers.

I gave you EXACT TESTED WORKING CODE.  What caused you to mark the grade down to a "B" -  I don't understand and would greatly appreciate an explanation of what was wrong with our solutions.

Standing by, ~Ray
0
 

Author Comment

by:jolly_joker
ID: 24152252
Hello,

 I couldnt manage to clear cookies with the provided phpcodes. cookies are still valid untill 2012 under the same domain i run phpcode. I managed to clear them with a js code

<script>
function cookieClear(){
  var cookie = document.cookie.split(/;\s*/);
  for(var i=0;i<cookie.length;i++){
    var cName = cookie[i].split('=')[0];
    document.cookie=cName+"=; path=/; expires=Thu, 01-Jan-1970 00:00:01 GMT;";
    document.cookie=cName+"=; expires=Thu, 01-Jan-1970 00:00:01 GMT;";
  }
}
</script>

Thats was why i gave B cuz i used another code however you were interested in my problem. Actually i dont know whats these grades for but if you tell me how to change it i can it.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 24152289
"Actually i dont know whats these grades for but if you tell me how to change it i can it."

Thanks for your consideration.  The grades directly affect our point scores and standing as Experts in the EE community.  You can click the "request attention" button near the lower right of the original post and ask a moderator to change the grade.

But to the larger issue - how are you testing and observing the cookies?  Are you using Firefox?  I'm wondering because I use that code all the time (thousands of times a day) and it always works for me, so there may be something wrong in your setup, or you set the cookies for one subdomain or protocol and you're trying to clear them in another, etc.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question