Simple Cookie example

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

  1. naveen

    naveen New Member

    Joined:
    Jun 2, 2005
    Messages:
    39
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Calcutta, India
    Home Page:
    http://naveenhere.blogspot.com
    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

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice