Session tracking in our CGI scripts : Cookie « CGI « Perl






Session tracking in our CGI scripts

    

#!/usr/bin/perl

use warnings;
use CGI;
use strict;
my $cgi=new CGI;
my $cookie=$cgi->cookie("myCookie");
if ($cookie) {
    print $cgi->header(); #no need to send cookie again
} else {
    my $value=generate_unique_id();
    $cookie=$cgi->cookie(-name=>"myCookie",
                         -value=>$value,
                         -expires=>"+1d"); #or whatever we choose
    print $cgi->header(-type=>"text/html",-cookie=>$cookie);
}
sub generate_unique_id {
    #generate a random 8 digit hexadecimal session id
    return sprintf("%08.8x",rand()*0xffffffff);
}

   
    
    
    
  








Related examples in the same category

1.A Simple Cookie Example
2.A Simple Cookie Example Using the CGI Module
3.Retrieving Cookies
4.Setting Cookie Expiration Without Using the CGI Module
5.Setting Cookie Expiration Using the CGI Module
6.Sending Multiple Cookies Using CGI.pm
7.Retrieving Multiple Cookies
8.Create cookie
9.Cookies and Session Tracking
10.Read cookie value
11.Program to read cookies from the client's computer
12.Time Period Abbreviations for the CGI Module's Header and Cookie Functions