I am trying to write a "member's area" for a web application that I am developing. I have followed the instructions in the CGI::Session Tutorial and Cookbook, but I think I am missing something. Actually I know I am, because I am having these problems, describe herein. After going to "index.html", the user submits to "login.cgi", which creates a new session, writes the session identification to a cookie in the user's browser and returns a page. The code below reflects that:
if(verify_password($username, $password)){ # write new HTTP session my $session = new CGI::Session(undef, undef, {Directory=>"/tmp"}); # inititialize session variables (expiry, etc) $session->param("~logged-in",1); #set logged in flag $session->param("username",$username); #write username in ses +sion $session->expires("~logged-in", "+5m"); #set 5 minute expirati +on # write sid to client cookie $cookie = $cgi->cookie(CGISESSID => $session->id); print $cgi->header(-cookie=>$cookie); print_success(); exit; } else { print_failure(); exit;}
The problem occurs when a user's session expires. They are sent back to "index.html" through "Location: index.html\n\n" in the HTTP header. The address bar still reports the location as being the referring page, however. So, when the user attempts to return to that script, (s)he is presented with the login prompt again. When the user reenters his/her information, they can once again freely navigate the site. Another interesting event is that if the user navigates "Back" from the second (erroneous) login prompt, and then attempts to visit the link again, they proceed. Below is the validation section of all the pages in the member's area.
#!/usr/bin/perl -wT use CGI; use CGI::Session; use CGI::Session qw/-ip-match/; use DBI; my $cgi = new CGI; my $session; my $anon = sub {$session->delete(); print"Location: /fwm/index.html\n\n"; exit(0);}; my $sid = $cgi->cookie("CGISESSID") || undef; $session = new CGI::Session(undef,$sid,{Directory=>'/tmp'}); &$anon unless $session->param("~logged-in"); # expired session $session->param("~logged-in",1); # refresh session
Below this code is all preprocessing for the page to be printed. The header that I print before the HTML is simply a "Content-type: text/html\n\n", because I don't want to write another cookie, or do I need to. Thanks in advance for the help.
amt

In reply to CGI::Session Expiration Woes by amt

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.