vladb has asked for the wisdom of the Perl Monks concerning the following question:
use strict; use lib qw(.); use CGI; use CGI::Cookie; my $cgi = new CGI; my $op = $cgi->param('op'); $main::cookie_name = $cgi->param('cookie_name'); $main::full_url = $ENV{SERVER_URL} . $ENV{SCRIPT_NAME}; $main::domain = $cgi->param('domain'); $main::path = $cgi->param('path'); $main::cookie_value = $cgi->param('cookie_value'); $main::domain ||= '.mybc.com'; $main::path ||= '/'; $main::cookie_value ||= 'na'; #$main::domain = $ENV{HTTP_HOST}; #($main::path) = ($ENV{SCRIPT_NAME} =~ m|(.*)/[^/]+$|); $main::cookie = $cgi->cookie($main::cookie_name); #$op='rm';$main::cookie=1; set_cookie($cgi) if ($op eq 'set'); remove_cookie($cgi) if ($op eq 'rm'); %main::cookies = fetch CGI::Cookie; my ($html_cookie_table, $html_cookie_select); parse_all_cookies({ html_table => \$html_cookie_table, html_select => \$html_cookie_select, }); print $cgi->header(); print qq| <html> <title>Cookie Test</title> <body> <pre> Cookie set/remove test. <form method=POST> <input type=hidden name=op value="$op"> <table border=0> <tr> <td></td><td></td> </tr> <tr> <td align=left>Name:</td> <td align=left nowrap> <input name=cookie_name value="$main::cookie_name"> <script> function select_cookie(sel_obj) { if (sel_obj.selectedIndex) { sel_obj.form.cookie_name.value = sel_obj.options[sel_obj.s +electedIndex].value; } else { sel_obj.form.cookie_name.value = ""; } } </script> <select name=cookie_name_select onChange="select_cookie(this);"> <option value=0>Select existing ...</option> $html_cookie_select </select> </td> </tr> <tr> <td>Value:</td><td><input name=cookie_value value="$main::cookie_valu +e"></td> </tr> <tr> <td align=left>Domain:</td><td align=left><input name=domain value="$ +main::domain"></td> </tr> <tr> <td align=left>Path:</td><td align=left> <input name=path value="$mai +n::path"></td> </tr> <tr><td colspan=2 align=center> <input type=button value="Remove Cookie" onclick="this.form.op.value += 'rm';this.form.submit();"> <input type=button value="Set Cookie" onclick="this.form.op.value = ' +set';this.form.submit();"> <input type=submit value="Refresh" onclick="this.form.op.value = '';" +> </td></tr> </table> </form> </pre> <hr> $html_cookie_table </body> </html> |; exit; ## ## SUBS ## sub set_cookie { my $cgi = shift; unless ($main::cookie) { $main::cookie = $cgi->cookie( -name => $main::cookie_name, -value => $main::cookie_value, -expires=> '+1h', -path => $main::path, -domain => $main::domain, ); print CGI::redirect(-location => $main::full_url, -cookie => [$mai +n::cookie]); exit; } } sub remove_cookie { my $cgi = shift; $DB::single = 1; if ($main::cookie) { my $cookie = $cgi->cookie( -name => $main::cookie_name, -value => 1, # also this one can't be 0 + or undef! # Setting expires to 'now' doesn't quite + work. # I have to hit the 'Remove Cookie' butt +on # 2/3 times (# of remove attempts) to ac +tually # remove the cookie. # -expires => 'now', # however, this works well all the time. # what's the trick? -expires=> 1, -path => $main::path, -domain => $main::domain, ); # print $cgi->header(); # print qq| # <pre> # REMOVING COOKIE: # -name => $main::cookie_name, # -value => 'foobar', # -expires=> 'now', # -path => $main::path, # -domain => $main::domain, # </pre> # |; print CGI::redirect(-location => $main::full_url, -cookie => [$coo +kie]); exit; } } sub parse_all_cookies { my ($r_html_table, $r_html_select) = @{$_[0]}{qw(html_table html_s +elect)}; $$r_html_table = qq| <table border=0 cellpadding=2 cellspacing=3 bgcolor=lightg +rey> <tr><td><b>Cookie Name</b></td><td><b>Content</b></td></tr +> |; foreach (keys %main::cookies) { $$r_html_select .= "<option value='$_'>$_</option>"; $$r_html_table .= "<tr><td>$_</td><td>".$main::cookies{$_}."</ +td></tr>"; } $$r_html_table .= "</table>"; }
| "There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(Ovid - code review) Re: CGI::cookie()
by Ovid (Cardinal) on Feb 20, 2002 at 21:26 UTC | |
|
Re: CGI::cookie() bugged? problems with removing cookies..
by screamingeagle (Curate) on Feb 20, 2002 at 20:41 UTC |