# Code snippet for Web Programming at 2013/11/13, # slide at http://zoro.ee.ncku.edu.tw/wp2013/res/08-cookie.pdf ################################################################################ # form version #!/usr/bin/perl -w use strict; use CGI; my $cgi = new CGI; my $state = defined $cgi->param('state') # the 'state' should match the term used in ? $cgi->param('state')+1 : 1; # if specified, operate accordingly; otherwise initialize print < END # show the state as well as send it to the server # the hidden type is exactly for this purpose # can we use method="get" here? # vi:nowrap:sw=2:ts=2 ################################################################################ # cookie version (with CGI) #!/usr/bin/perl -w use strict; use CGI; use CGI::Cookie; my $cgi = new CGI; my $state = defined $cgi->cookie('state') # the 'state' should match the term used in ? $cgi->cookie('state') : 1; # if specified, operate accordingly; otherwise initialize 1 == $cgi->param('next') and ++$state and print "Set-Cookie: state=$state\n"; # update cookie if required 1 == $cgi->param('clean') and $state = 1 and print "Set-Cookie: state=\n"; # clean cookie print <
END # use two forms to reduce extra actions, such as a radio button # vi:nowrap:sw=2:ts=2 ################################################################################ # cookie version (with JavaScript) Cookie demo The state is 1 ################################################################################ # JavaScript version (without web storage) Cookie demo The state is 1