sloppycode.net
Cookie example
Getting and setting cookies in Perl.
Home
›
Code snippets
›
Perl (CGI)
›
Cookie example
This small example illustrates how use cookies in Perl and the fields available.
#!/usr/bin/perl -w use CGI qw/:standard/; use CGI::Cookie; use CGI::Carp qw(fatalsToBrowser); # -- Make the cookie ----------- # expires - Easiest method is to +12M for 12 months. See CGI.pm's documentation for details. # domain - The domain its being set for, Defaults to the web server that is setting the cookie # path - Directory path the cookie should be used for. Defaults to "/" # secure - Use 1/true if the cookie should only be set over a secure connection. $cookie1 = new CGI::Cookie(-name => 'foo', -value => 'bar', -expires => '+3M', -domain => '.sloppycode.net', -path => '/', -secure => 0 ); $cookie2 = new CGI::Cookie(-name => 'foo2', -value => 'bar2', -expires => '+3M', -domain => '.sloppycode.net', -path => '/', -secure => 0 ); # -- Set the cookie ----------- # Use ; to seperate cookies out print "Set-Cookie: $cookie1;Set-Cookie: $cookie2;\n"; # -- HTML header ----------- print "Content-type: text/html\n\n"; print "<html><body>"; # -- Retrieve a cookie ----------- %cookies = fetch CGI::Cookie; $cookie1 = $cookies{'foo'} ->value; $cookie2 = $cookies{'foo2'} ->value; # NB - the page will need to be refreshed before the cookie's values can be retrieved print $cookie1 . "<BR>"; print $cookie2 . "<BR>"; # -- End of HTML ----------- print "</body></html>";
{Name}
Says:
{Date}
{Text}
› Home
› C#
› Snippets
› Articles
› Tools
› Taglines
› ASP
› Dictionary Object
› FSO
› Unix cheat sheet
› Gaming
› CSS
› Yak
› Umbraco
› About
› Contact
› Privacy
› Projects
› Search
› Sitemap
Buy on Amazon
Buy on Amazon
Buy on Amazon
Buy on Amazon