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

I'm trying to delete cookies using CGI.pm. I found a couple of places on this site that talk about setting the expiration to a negative value in the past. This doesn't seem to work. Can anyone tell me how to do it?

Replies are listed 'Best First'.
Re: Deleting cookies
by tachyon (Chancellor) on Jul 20, 2001 at 04:30 UTC

    If it was your post as Anonymonk yesterday you will already have seen the answer I gave at Re: Weird CGI Cookie Problem. This does work so if it is not working for you you will need to explain your program logic with a example of what you are doing and details of how it does not work in precise terms.

    As noted in the node above to delete a cookie you simply overwrite the original with a cookie with the same name from the same domain and path that has an expiry in the past. What happens is that this new cookie info is sent to the client browser in the HTTP header. The browser parses the cookie info and then writes the new cookie. As each domain/path can only have one cookie of a given name this new cookie will overwrite the old one.

    Because the browser is handling this interaction you do not have direct control over what happens. It may choose to delete the cookie immediately, perhaps store the new cookie in memory to be discarded when the browser shuts down (as happens if no expiry is specified) or do nothing if cookies have been disabled. The bottom line is that you are depending on the browser to do the deletion. If it is really vital to delete a cookie don't send it in the first place. Send an ID number cookie that references the data you want perfect deletion control over. *You store this data on the server* Then all you need do is delete the server data and let the cookie expire in it's own time.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Deleting cookies
by simon.proctor (Vicar) on Jul 20, 2001 at 12:05 UTC
    The expiration method does work - there are a few things you can try to track it down (at the time of my reply theres no code to cast my eye over :P ).

    First and foremost you need to determine that the cookie is being set by the script. Ok this sounds a little obvious, however I have to use PWS at work (tell me about it) and its very kind to force a standard content header the moment I print something. Bye bye valid headers - bye bye printing that cookie.

    I assume you are using plain old CGI for setting the cookie? If you aren't you should be. I have used CGI::Cookie without problem in the past but recently it 'appears' to not be working. Without changing anything else other than using CGI my code functions again.

    The code I use is as follows:

    my $query = CGI->new(); my $cookie = $query->cookie( -name => 'forms', -value => '', -path => "/" -expiry => "-1d" ); print $query->header(-cookie=>$cookie);


    Hopefully that will work for you. Remember to change the cookie name to that of the one you set to log the details in. If not - please post some snippets here :)

    Update:

    On an additional note - I would recommend that you completely clear you cache. If you use IE (hey! some of us do) then you must go into the temporary internet files folder and clear everything. You must ensure that all of your cookie files are physically deleted. Don't rely on your browser to do it. </code>
Re: Deleting cookies
by dvergin (Monsignor) on Jul 20, 2001 at 03:04 UTC
    Very good. You have done your own search for explanations and examples. You have correctly understood the essence of the solutions posted elsewhere. You have tried it yourself.

    Next step: post your best attempt here so we can perhaps see what is going wrong.