I'm programming a website with authentication using CGI::Session. Everything seems to go well on the authentication page, though when it moves on to the next page the page doesn't seem to be recognising that a session is open...! frustrating to say the least...

I'm following this tutorial on cpan - this is how I'm initialising the session:

$session = new CGI::Session(undef, undef, {Directory=>'./tmp'}) or die + CGI::Session->errstr;
After that I have an if-else clause, with the cgi acting accordingly to what the user clicked on:
my $action = $cgi->param("submit"); if($action eq "Sign in") { #If user signing in $cookie = $tools->sign_in($cgi,$dbh,$session); if($cookie eq "0") { #Login failed print $cgi->header; print $cgi->start_html(-title=>"Home Page"); print "Login failed"; $session->delete; $logged = 0; } else { #Login successful print $cgi->header(-cookie=>$cookie); print $cgi->start_html(-title=>"Home Page"); print "Welcome ".$session->param("user_name"); print $cgi->start_multipart_form(-method=>'post', -action=>'main.pl', -name=>'sign_out'); print $cgi->submit(-name=>'submit',-value=>'Sign out'); print $cgi->end_form; $session->expire('+30m'); $logged = 1; } } else { print $cgi->header; print $cgi->start_html(-title=>"Home Page"); }
The sign_in method returns 0 if authentication was unsuccessful, and a cookie otherwise and creates a sign_out button.

The session expires in 30 mins and is present in the file is present in the right directory.

Once the user is signed in, he can select a radio button and open another page - here I always get an empty session!

This is the next page's session initialisation, and is pretty much straight from the tutorial:

my $session = CGI::Session->load or die CGI::Session->errstr; print $session->header; if ($session->is_expired) { print $session->header, $cgi->start_html, $cgi->p("Your session timed out. Click here to start a new sessio +n."), $cgi->end_html; exit(0); } if($session->is_empty) { print $cgi->start_html; print "Click here to sign in"; print $cgi->end_html; exit(0); }
It always goes to the is_empty clause. Any idea what I'm doing wrong?

Thanks!


In reply to Perl CGI::Sessions by dariusj

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.