Solved

Delete all cookies created by the webpage

Posted on 2009-04-15
7
581 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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 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…

688 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