#!/usr/bin/perl -T # # the main page. /cgi-bin/main.pl # use strict; use warnings; use CGI; my $q = new CGI; my $cookie = $q->cookie( -name => 'session_id' ) || set_and_test_cookie( $q ); # if you get here, you have a cookie # use cookie for whatever... exit; sub set_and_test_cookie { my $q = shift; my $server = $q->server_name; my $session_id = $ENV{UNIQUE_ID}; # change to taste my $cookie = $q->cookie( -name => 'session_id', -value => $session_id, -path => '/cgi-bin', -secure => 1, ); print $q->redirect( -url => "https://$server/cgi-bin/cookie_test.pl", -cookie => $cookie, ); exit; } # # end main.pl # #!/usr/bin/perl -T # # check the cookie. /cgi-bin/cookie_test.pl # use strict; use warnings; use CGI; use constant SOURCE_CGI => "/cgi-bin/main.pl"; my $q = new CGI; my $cookie = $q->cookie( -name => 'session_id' ); if ( defined $cookie ) { print $q->redirect( -url => SOURCE_CGI ); # ok, go back. } else { print $q->header( -type => 'text/html' ), $q->start_html( "Cookies Disabled" ), $q->h1( "Cookies Disabled" ), $q->p( "Your browser is not accepting cookies. You need them. Enable them and ", $q->a( { -href => SOURCE_CGI }, "Try Again" ), '.' ), $q->end_html; } # end cookie_test.pl