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


Can a cookie that was created via be deleted by CGI::SESSION?

If so, how?

Thanks in advance.

  • Comment on Can a cookie created via be deleted by CGI::SESSION?

Replies are listed 'Best First'.
Re: Can a cookie created via be deleted by CGI::SESSION?
by Joost (Canon) on Jul 03, 2006 at 05:44 UTC
      Thanks for your reply.

      I'm customizing an existing script created by someone else.

      The cookie was created via, but I want to delete the cookie after using it to bypass an error message that occurs and shuts down the processing of the script after a 'refresh' when I delete an entry in the db...

      The script has a parameter that looks for the corresponding entry in the db and halts when the db entry is not found. The only way I see to get around it is to delete the corresponding cookie so it has nothing to compare it to and let the program progress as normal by creating another parameter and corresponding db entry. So far, I know CGI::SESSION has the delete().

        CGI::Session::delete() really just does this:
        my $cgi = CGI->new(); my $cookie = $cgi->cookie( -name=>$self->name, -value => 1, -expires=> + '-1d');
        And then later it sends the header including this cookie definition back to the browser. (example later in this post)

        The cookie isn't really managed by perl, it is managed by the browser. By telling the browser that the cookie was supposed to expire a day ago you can cause it to be deleted.

        This is done with the http headers that you send back with your response.

        It is easier to do this using one of the standard modules instead of writing the "Set-Cookie" header manually.

        Basic Example:

        my $cgi= CGI->new(); my $cookie = $cgi->cookie( -name=>$name, -value => 1, -expires=> '-1d' +); print $query->header(-cookie => $cookie, type => 'text/html'); print "This text is sent to the browser, and at the same time the cook +ie is cleared\n";
        Note that this is setting what the value of the cookie will be -after- the browser gets this response.

        If you want to remove the cookie for -this- invocation you could modify the value of $ENV{HTTP_COOKIE}. For example, this would remove all cookies for -this- run - but not future ones.

        delete $ENV{HTTP_COOKIE};

        I'm sure there is a module for modifying the current cookie definitions, but I can't think of one offhand and am too tired to search at the moment :)

        Hope this helped.

Re: Can a cookie created via be deleted by CGI::SESSION?
by jbrugger (Parson) on Jul 03, 2006 at 06:33 UTC
    If you use a module, do you read the documentation?
    It's right there on cpan: CGI::Session
    delete() Deletes a session from the data store and empties session data fro +m memory, completely, so subsequent read/write requests on the same o +bject will fail. Technically speaking, it will only set object's stat +us to STATUS_DELETED and will trigger flush(), and flush() will do th +e actual removal.
    Update: Whoops, i did not read your question well enough, sorry :$.

    Yes you can, just re-create the cookie and delete it (but the answer was already given.

    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
      Thanks folks.

      I'll give it a try.