Solved

Delete all cookies created by the webpage

Posted on 2009-04-15
7
571 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 108

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 108

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 108

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now