webstudioro has asked for the wisdom of the Perl Monks concerning the following question:

I have a problem and i don't why. The problem is with delete cookie. I don't why do not delete all cookies. :(
If you see what is the problem please tell me. Thank you for your time.

#!perl -wT use CGI::Carp('fatalsToBrowser'); use warnings; use CGI qw(:standard); use CGI::Cookie; use CGI; @decodareCookie = ('\%3A','\%20'); %decodareCookie = ('\%3A',':'); $regCode = 01; $nameCookie = "Test"; $query = CGI->new(); print header(-cookie=>$cookie); # Make cookie 1 my $cookieListA = join(":",$nameCookie,$regCode,10); my $listA = join(":",Product1,5); my %cookie = CGI::Cookie->fetch; my $cookie = CGI::Cookie->new(-name=>$cookieList,-value=>$list); # Make cookie 2 my $cookieListB = join(":",$nameCookie,$regCode,11); my $listB = join(":",Product2,1); my %cookie = CGI::Cookie->fetch; my $cookie = CGI::Cookie->new(-name=>$cookieList,-value=>$list); # Make cookie 3 my $cookieListC = join(":",$nameCookie,$regCode,12); my $listC = join(":",Product3,2); my %cookie = CGI::Cookie->fetch; my $cookie = CGI::Cookie->new(-name=>$cookieList,-value=>$list); print "$ENV{'HTTP_COOKIE'}<br><br>"; # Read each cookie and decode foreach (split(/; /,$ENV{'HTTP_COOKIE'})) { ($cookie,$value) = split(/=/); foreach $caracter (@decodareCookie) { $cookie =~ s/$caracter/$decodareCookie{$caracter}/g; $value =~ s/$caracter/$decodareCookie{$caracter}/g; ($idProgram, $nxRegCode, $nxIdProdus) = split (/:/, $cookie); ($nxDenumireP, $canditate) = split (/:/, $value); } if (($idProgram eq $nameCookie) && ($nxRegCode eq $regCode)) { # Delete all cookies my $cookieList = join(":",$idProgram,$nxRegCode,$nxIdProdus); my $list = $canditate; my %cookie = CGI::Cookie->fetch; my $cookie = CGI::Cookie->new(-name=>$cookieList,-value=>$list,-expire +s=>'-1h'); print "Delete cookies : $regCode $nxIdProdus $canditate $nxPretP | "; } }

Replies are listed 'Best First'.
Re: Problem to delete a cookie
by jonadab (Parson) on Oct 19, 2003 at 11:16 UTC

    If I understand your code correctly, you're trying to tell the user's browser that it should delete the cookies. Of course, the user's browser is free to take your advice and delete them, or it could ignore you and keep them until the end of time. You can tell the browser to delete them as a courtesy, so that a well-behaved browser does not need to continue to store them, but you also should invalidate or delete the cookies on the server side, so that if the browser should happen to continue to present them they will be ignored.

    I realise this doesn't answer your question, because what you really want to know is whether there's a problem with your code that's causing the browser to fail to understand what you're telling it. However, I don't really know. I am personally unfamiliar with the "Delete cookies" header. According to this (fairly old) document at Netscape, the way to delete a cookie is to re-issue it with the same name, but with an expiration date in the past. This is probably the oldest way to delete a cookie and so therefore probably has the widest browser support, and it seems to be what you are doing (on the line right before the "Delete Cookies" line). That should work in most browsers, but you may have to wait for the browser to be closed and restarted before it will take effect. Also, if the browser in question has a run-continuously-in-the-background feature, "closed and restarted" may mean the computer may need to be rebooted, though I'm not certain about that. As far as the "Delete Cookies" header, I don't know what browsers (if any) support it. Or is that just information for you or the user to know what's going on?

    update: I just noticed that you are trying to do them all in one fell swoop by using join to lump the cookies all together into one. That may not work. You may need to expire each one separately by calling CGI::Cookie->new once for each cookie. Beyond that, I don't know.


    $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
Re: Problem to delete a cookie
by jeffa (Bishop) on Oct 19, 2003 at 15:22 UTC

    First, please start learning how to search the site, as this questions has been asked many time: delete cookie. You can find answers to the immediate question at How can I delete a cookie?

    Second, i gave you cookie code over at (jeffa) 7Re: CGI.pm error and (jeffa) 3Re: CGI.pm error many moons ago. Looks like you didn't understand any of it, so lets start over.

    You first get any cookies that might be available:

    my %cookie = CGI::Cookie->fetch;
    Then you see if the key you are looking for is there, we will use 'test' since this is a test:
    if (exists $cookie{test}) { # we have our cookie, do something with $cookie{test}->value } else { # we don't have a cookie, need to make one my $cookie = CGI::Cookie->new(-name=>'test',-value=>12345); # now we "inject" into the HTTP headers print header(-cookie=>$cookie); }
    You do not print the HTTP header first without seeing if there are cookies or not. Once you print the HTTP header, game over. If you needed to do some cookie munging, you can't. You have to get the cookie first, modify whatever you need to, then "send if off" in the HTTP header (via print header()).

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      ok. I see you examples, but please tell me something.
      I have more cookies and the $ENV{'HTTP_COOKIE'} look something like this (ex: Steel%3A01%3A11=Noul%20Produs2%3A1%3A250%3A; Steel%3A01%3A10=Noul%20Produs%3A1%3A5000%3A)
      , and i want to delete all my dates from $ENV{'HTTP_COOKIE'} with my script (please look my script), but this stupid script don't want , WHY?
      I want the script to look on all my cookies and delete all (only cookies with $idProgram eq Steel and $nxRegCode eq 01), one by one.
      Please help me man. Thank you.

      #!perl -wT use CGI::Carp('fatalsToBrowser'); use warnings; use CGI qw(:standard); use CGI::Cookie; use CGI; $query = CGI->new(); my %cookie = CGI::Cookie->fetch; @decodareCookie = ('\%3A','\%20'); %decodareCookie = ('\%3A',':','\%20','##'); foreach (split(/; /,$ENV{'HTTP_COOKIE'})) { ($cookie,$value) = split(/=/); foreach $caracter (@decodareCookie) { $cookie =~ s/$caracter/$decodareCookie{$caracter}/g; $value =~ s/$caracter/$decodareCookie{$caracter}/g; ($idProgram, $nxRegCode, $nxIdProdus) = split (/:/, $cookie); ($nxDenumireP, $canditate) = split (/:/, $value); } if (($idProgram eq Steel) && ($nxRegCode eq 01)) { my $cookieList = join(":",Steel,$nxRegCode,$nxIdProdus); my $list = join(":",$nxDenumireP,$canditate); my $cookie = CGI::Cookie->new(-name=>$cookieList,-value=>$list,-expire +s=>'-1h'); print header(-cookie=>$cookie); print "<center><h2>Produs adaugat pentru plata</h2></center><br>"; print "<br>$ENV{'HTTP_COOKIE'}"; } }