?
Solved

Delete all cookies created by the webpage

Posted on 2009-04-15
7
Medium Priority
?
586 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 1000 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 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
Tutorial: Introduction to Managing a Linux Server

In this tutorial on systemd, we will explore:
-OS/Distro Adoption
-chkconfig and Other Legacy Commands
-Summary and Key Commands

 
LVL 111

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 111

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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

762 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