in reply to Re^6: Cookie->fetch problem
in thread Cookie->fetch problem

Here is a small perl cookie test script. Put this cookietest.cgi in the same folder as manage_users.cgi. Access with browser and click the create button several times to see cookies created. Then repeat press the refresh button to see them expire after 20 seconds and disappear. This should show whether the problem is with your script, browser or server.

#!/usr/bin/perl # cookietest.cgi use strict; use CGI ':standard'; use CGI::Cookie; use Data::Dumper; my $time = scalar localtime; my $cookie; if ( param('action') ){ $cookie = new CGI::Cookie( -name => (sprintf "TESTCOOKIE_%05d",rand(10000)), -value => $time, -expires => '+20s' ); CGI::delete_all(); print redirect( -cookie => $cookie ); } my $JS =<<EOJ; function display_ct() { var x = new Date() document.getElementById('ct').innerHTML = x; } EOJ print header( -cookie => $cookie ), start_html( -title => "cookietest.cgi", -script=>$JS, -onLoad=>"display_ct();" ), h2("Cookie Test"), h3("Server time is : $time"), h3("Client time is : <span id='ct' ></span>"); print start_form( -method => "POST" ); print submit("action","create cookie"); print submit("refresh","refresh"); print end_form; my %cookie = CGI::Cookie->fetch; my $count = keys %cookie; print h3("There are $count cookies"); print pre( Dumper \%cookie ),end_html;
poj

Replies are listed 'Best First'.
Re^8: Cookie->fetch problem
by huck (Prior) on Mar 12, 2017 at 00:42 UTC

    The redirect call threw me, i guess cuz it didnt have a text uri or a -uri=> section. So i ran it, and it all became clear. Ill remember that "trick" now.

    Then in researching it http://perldoc.perl.org/CGI.html#GENERATING-A-REDIRECTION-HEADER i found "All names arguments recognized by header() are also recognized by redirect(). However, most HTTP headers, including those generated by -cookie and -target, are ignored by the browser.". but mine (firefox51) didnt ignore the -cookie => $cookie part.

      However, most HTTP headers, including those generated by -cookie and -target, are ignored by the browser.". but mine (firefox51) didnt ignore the -cookie => $cookie part.

      General statements like that may have been true more than 20 years ago when CGI.pm first appeared

      The documentation is gigantic and not maintained , for anyone who wants to learn HTTP should learn from the RFC