0

I am using CGI.pm to write out cookies. Now during the course of the user using my site, other cookies are added to the "test.com" cookie set, (as shown in the broswer history)

But now I want to log the user out, and "clean" the PC. Since I don't know what scripts the user has used, I can't foresee what cookies would be on the PC.

In short, it there a way to read all the cookies for "test.com" back into a script so I can then print them out again with a 1s duration, (effectively 'deleting' them) ** I know you can read the cookie back in with $xyz=cookie('$name') ... but how can I create the array holding the $name variable so I can loop through it? The script will also run on "test.com", so the cross site policy is not an issue

+++++ brian d foy added a partial answer below. So this how I envisage the code might be strung together.

use CGI::Cookie;

%cookies = CGI::Cookie->fetch;
for (keys %cookies) {
$del_cookie.="cookie(-NAME=>'$cookies[$_]',-PATH=>'/',-EXPIRES=>'+1s');";
}
print header(-cookie=>[$del_cookie]);

I wondered how the script would recognise the domain. Appears the script is intelligent enough to only load the cookies for the domain for which the script is being executed on. (Now I've just got to find out why Firefox doesn't delete expired cookies!! Just found some listed that expired 29th - 31st Jan within my test domain, and at first wondered why they didn't appear in my cookie list!)

Community
  • 1
  • 1
Cristofayre
  • 121
  • 1
  • 11
  • 2
    http://search.cpan.org/~markstos/CGI.pm-3.64/lib/CGI/Cookie.pm#Recovering_Previous_Cookies – mpapec Jan 31 '14 at 19:35

2 Answers2

0

If you are trying to do this from your CGI script, you'll only have access to the cookies for that domain. You can get that list and reset them by giving them a time in the past.

It sounds like you aren't asking a cookie question at all. You're asking how to make an array. The CGI::Cookies (which comes with CGI.pm) has an example to deal with all the cookies you have access to under that domain:

%cookies = CGI::Cookie->fetch;
for (keys %cookies) {
    do_something($cookies{$_});
    }
brian d foy
  • 129,424
  • 31
  • 207
  • 592
  • Somehow, my reply didn't get posted (still learning!). I did know i can only read one domain as I quoted in last sentence, (but thanks anyway) It's a mixed question of both cookies and arrays as didn't know how to read the former into the latter. I have re-edited my post using your snippet, so wonder if you could advise on that – Cristofayre Feb 02 '14 at 10:58
0

This is what I ended up with:

use CGI::Cookies;
%cookies = CGI::Cookie->fetch;
@cookie  = keys %cookies;  
for($x=0; $x<@cookie; $x++){
my $c = CGI::Cookie->new(-name => $cookie[$x],-value => '-',-expires => '+1s');
print "Set-Cookie: $c\n";
}
print "content-type: text/html\n\n";

Firefox still leaves the cookies intact, (apparently that's a "design issue" and not a bug!!) but they are reset to a void value, and set to expire / become redundant in 1 second. Plus, quite why the "print" statement being sent before the "content-type" header doesn't cause a server error I don't know. OK, so purists will probably find a simpler system, and use "foreach" rather than for/next loop ... but I understand how the latter works!

Cristofayre
  • 121
  • 1
  • 11
  • Some browsers don't do the actual cleanup until they exit. – brian d foy Feb 02 '14 at 12:57
  • That's the behaviour I would expect, but Firefox holds on to them; I have some that date back to sites I visited in November!! (Dates expired, but still physically there) Only way to get rid is manual delete. Even if I set it to use a custom setting in options, it will jump back to "Remember History" option! No probs. Just summat you live with. From what I read, they say its so you can jump back to a tab if browser crashes – Cristofayre Feb 03 '14 at 13:45