Set the cookie like:

use CGI; my $q = new CGI; sub set_cookie { my ( $username ) = @_; my $cookie = $q->cookie( -name => 'validated', -value => $username, -expires => '+1h', ); print $q->header( -cookie=>$cookie ); }

This sets a cookie called 'validated' which contains the users name and expires in one hour. If you keep resetting it the 1 hour session contiunes until 1 hour of disuse.

Check the cookie like this:

# returns username stored in cookie if we have validated user or 0 if +not sub validate { return $q->cookie('validated') if $q->cookie('validated'); return 0; }

You don't really need to put the password in the cookie but a checksum is a good idea so it can't be spoofed. Consider how your site (won't) work if cookies are disabled. Another non cookie approach is to use 'hidden' fields and a checksum (like the one you need to put in your cookie)

use CGI; use Crypt::Blowfish; use Crypt::CBC; my $q = new CGI; my $c = new Crypt::CBC( 'gnuisnotunix','Blowfish'); my $hidden_fields = ''; if ( validate( $q->param('username'), $q->param('password') ) { my $hidden_fields = get_hidden_fields( $q->param('username'); show_database_form($hidden_fields) } else { error( 'Invalid username/pass' ); } sub validate { my ( $username, $password ) = @_; # validate using the user/pass return 1 if $username eq 'foo' and $password eq 'bar'; # alternatively validate on the hidden fields return 1 if validate_checksum(); return 0; } sub get_hidden_fields { my $username = shift; my $checksum = $c->encrypt_hex($username); return <<HTML; <input type="hidden" name="username" value="$username"> <input type="hidden" name="checksum" value="$checksum"> HTML; } sub validate_checksum { return $c->decrypt_hex($q->param('checksum')) eq $q->param('userna +me') ? 1 : 0; } sub show_database_form { my $hidden = shift; return <<HTML; <form method="POST" action="$MY_CGI"> $hidden blah blah <input type="submit" value="Submit" name="Submit"> </form> HTML }
cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print


In reply to Re: Looking for CGI session guidance by tachyon
in thread Looking for CGI session guidance by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.