package Book::Cookie2; use Apache::Constants qw(:common); use strict; use Apache::Request (); use Apache::Cookie (); use vars qw($r $q $switch $status $sessionID); sub handler { $r = shift; init(); print_header(); print_status(); return OK; } sub init { $q = Apache::Request->new($r); $switch = $q->param("switch") ? 1 : 0; my %cookies = Apache::Cookie->fetch; $sessionID = exists $cookies{'sessionID'} ? $cookies{'sessionID'}->value : ''; # 0 = not running, 1 = running $status = $sessionID ? 1 : 0; # switch status if asked to $status = !$status if $switch; if ($status) { # preserve sessionID if exists or create a new one $sessionID ||= generate_sessionID() if $status; } else { # delete the sessionID $sessionID = ''; } } sub print_header { my $c = Apache::Cookie->new( $r, -name => 'sessionID', -value => $sessionID, -expires => '+1h'); # Add a Set-Cookie header to the outgoing headers table $c->bake; $r->send_http_header('text/html'); } # print the current Session status and a form to toggle the status sub print_status { print qq{