1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Simple Cookie example

Discussion in 'Perl' started by naveen, Jun 18, 2005.

  1. naveen

    naveen New Member

    Hi friends,
    This is a very simple example demonstrating how to implement cookies in perl.

    This File (set.pl) sets the cookie-

    Code:
    #!/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:
    
    #!/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.
     

Share This Page