Simple Cookie example

naveen's Avatar author of Simple Cookie example
This is an article on Simple Cookie example in Perl.
Hi friends,
This is a very simple example demonstrating how to implement cookies in perl.

This File (set.pl) sets the cookie-

Code: PERL
#!/usr/bin/perl 

use CGI qw(:standard);   
use CGI::Cookie;

$q=new CGI;


$cookie= $q->cookie(-name=>'DName', -value=>'login_time', -expires=>'+72h', -path=>'/');
   
print "Set-Cookie: $cookie\n";     
    
print "Content-type: text/html\n\n";

print "Cookie set Successfully";

exit;
Running the above file from a browser will set a cookie in your machine.

This File (get.pl) retrieves the cookie and checks it-
Code: PERL
#!/usr/bin/perl   
     
use CGI qw(:standard);   
use CGI::Cookie;

print "Content-type: text/html\n\n";

$q=new CGI;

$fetch= $q->cookie(-name=>'DName');   
if ($fetch eq 'login_time')
{
    print "OK..!!";
}
else
{
    print "Sorry";
}
More advanced authentication can be done by using other perl modules such as CGI::Session and Apache::Cookie.