programming-examples/perl/CGI/Session tracking in our CGI scripts.pl

20 lines
583 B
Perl
Raw Normal View History

2019-11-15 12:59:38 +01:00
#!/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);
}