Solved

Delete all cookies created by the webpage

Posted on 2009-04-15
7
578 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 110

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 110

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Integrating blogs between to separate WordPress websites 4 33
how can i break an array into a couple 5 26
Aspx calendar pop up control 3 28
REST call Failing 1 15
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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

726 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