Perl CGI  «Prev  Next»

Lesson 11Expiring cookies
ObjectiveExamine how to Expire a Cookie in Perl.

Examine how to expire a Cookie in Perl

To expire a cookie in Perl 6 (now officially known as Raku), you can use the `HTTP::Cookie` module to create a cookie with an expiration time set in the past. Here's how you can do it:
use HTTP::Cookie;

# Create a cookie and set it to expire
my $cookie = HTTP::Cookie.new(
    name  => 'my-cookie',
    value => '',
    path  => '/',
    expires => DateTime.now - 1  # Set expiration to a past date
);

# Output the Set-Cookie header
say "Set-Cookie: " ~ $cookie.Str;

Explanation:
  1. HTTP::Cookie Module: Used to manage cookies in Raku.
  2. Set expires Attribute: Setting the expires attribute to a past date effectively tells the browser to remove the cookie.
  3. Path Attribute: Ensures the cookie is invalidated for the specific path.

When the browser receives the `Set-Cookie` header with an expired date, it will remove the cookie from the client-side storage. Ensure that the path and domain match the original cookie for successful removal.


Cookie persistence


SEMrush Software