Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Perl (http://www.go4expert.com/articles/perl-tutorials/)
-   -   Simple Cookie example (http://www.go4expert.com/articles/simple-cookie-example-t365/)

naveen 18Jun2005 13:10

Simple Cookie example
 
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.


All times are GMT +5.5. The time now is 14:00.