#!/usr/bin/perl use strict; use warnings; use CGI; my $q = CGI->new(); our $cookie = ''; if ( defined $q->cookie('cookiename') ) { $cookie = $q->cookie('cookiename') # validate cookie here based on whatever } elsif ($q->param('some_field_name') { # form submitted $cookie = 'whatever'; } else { # show the html form } #### # at beginning of script use CGI; our @cookie_jar = (); our $q = CGI->new(); # when you want to set a cookie push @cookies, $q->cookie(-name=>'sessionID', -value=>'xyzzy', -expires=>'+1h'); # when you print a page, instead of calling $q->header # use this instead print cookie_header; # which sends all cookies sub cookie_header { return $q->header(-cookie=>\@cookie_jar); } # if you're on mod_perl, you also need to empty # the cookie jar here, so amend sub cookie_header { my header = $q->header(-cookie=>\@cookie_jar); @cookie_jar = (); return $header; }