JCHallgren has asked for the wisdom of the Perl Monks concerning the following question:

Quite new to this (HTML/PERL) but have just read most ALL the SSI Faq links..
Have a simple chatroom based on a variant of a guestbook script...works fine..Thanks to some help from this forum!...but would like to save users name/city/st (after a succcesful posting) so that when they return again, i might (assuming cookie was there) preload those fields in FORM data as initial values...

Have a main index page...it links to the "ADD" page...would using SSI on ADD page to do a "include virtual cookie.pl" type req work? So that it would read cookie data and modify the HTML for the page? But then...wouldn't I have to have all the ADD page code embeded in the PL pgm? Which makes the ADD page hard to maintain...

How should i approach this issue? Is there a better way?(I have looked at postings about cookies here but did not see any samples about this issue.) I want to have this secure as much as possible also! Hope you understand my questions! THANKS
  • Comment on How to preload user data on form via cookie & Perl/CGI?

Replies are listed 'Best First'.
Re: How to preload user data on form via cookie & Perl/CGI?
by zentara (Cardinal) on Jan 31, 2005 at 14:03 UTC
    Well here is an old example by Lincoln Stein(CGI.pm author). I'm sure it's somewhere on the net, but I couldn't find a link so I'll post it here. The all you need to add is encryption of the cookie( if needed).

    Also check out CGI docs and search for cookies.

    #!/usr/bin/perl use CGI qw(:standard); @ANIMALS=sort qw/lion tiger bear pig porcupine ferret zebra gnu ostric +h emu moa goat weasel yak chicken sheep hyena dodo lounge-lizard squirrel rat mouse hedgehog racoon baboon kangaroo hippopotamus giraffe/; # Recover the previous animals from the magic cookie. # The cookie has been formatted as an associative array # mapping animal name to the number of animals. %zoo = cookie('animals'); # Recover the new animal(s) from the parameter 'new_animal' @new = param('new_animals'); # If the action is 'add', then add new animals to the zoo. Otherwise # delete them. foreach (@new) { if (param('action') eq 'Add') { $zoo{$_}++; } elsif (param('action') eq 'Delete') { $zoo{$_}-- if $zoo{$_}; delete $zoo{$_} unless $zoo{$_}; } } # Add new animals to old, and put them in a cookie $the_cookie = cookie(-name=>'animals', -value=>\%zoo, -expires=>'+1h'); # Print the header, incorporating the cookie and the expiration date.. +. print header(-cookie=>$the_cookie); # Now we're ready to create our HTML page. print start_html('Animal crackers'); print <<EOF; <h1>Animal Crackers</h1> Choose the animals you want to add to the zoo, and click "add". Come back to this page any time within the next hour and the list of animals in the zoo will be resurrected. You can even quit Netscape completely! <p> Try adding the same animal several times to the list. Does this remind you vaguely of a shopping cart? <p> <em>This script only works with Netscape browsers</em> <p> <center> <table border> <tr><th>Add/Delete<th>Current Contents EOF ; print "<tr><td>",start_form; print scrolling_list(-name=>'new_animals', -values=>[@ANIMALS], -multiple=>1, -override=>1, -size=>10),"<br>"; print submit(-name=>'action',-value=>'Delete'), submit(-name=>'action',-value=>'Add'); print end_form; print "<td>"; if (%zoo) { # make a table print "<ul>\n"; foreach (sort keys %zoo) { print "<li>$zoo{$_} $_\n"; } print "</ul>\n"; } else { print "<strong>The zoo is empty.</strong>\n"; } print "</table></center>"; print <<EOF; <hr> <ADDRESS>Lincoln D. Stein</ADDRESS><BR> <A HREF="./">More Examples</A> EOF ; print end_html;

    I'm not really a human, but I play one on earth. flash japh
Re: How to preload user data on form via cookie & Perl/CGI?
by zentara (Cardinal) on Jan 31, 2005 at 13:31 UTC
    Are you trying to do something like save the "username" and "city" in a cookie, and have it automatically load the values in a form when a use returns? If so, think about it step by step. First, you need a form to get the data, and set it as an encrypted cookie. Then when the user returns a second time, the cgi should detect if the cookie exists, get the cookie, and load the values into the form as defaults. I don't have a simple example handy, maybe someone else does. Just about every bbs system does something like this.

    I'm not really a human, but I play one on earth. flash japh
      That is precisely what I am trying to do... But the questions still are: Is this the best way to do it? And wouldn't it require that I create all the ADD page code on the fly?
        Is it the best way? How else are you going to do it? Some would say it's better to keep the info in a database, because some people don't allow cookies, and it prevents cookie tampering, but for a chatroom, maybe it dosn't matter? Of course you will have to create all the pages on the fly, but that is what cgi is all about. You can greatly reduce the amount of work the cgi has to do, by making pre-made templates of pages, and just adding the custom cookie data to it. Most monks recommend Html::Template, but for simple pages, you can put the html in "here docs" and just fill in the $username and $city variables "on the fly". There are plenty of here-doc examples around.

        I'm not really a human, but I play one on earth. flash japh