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

hi guys, i've got some doubts about using the module CGI::Session, i've got a couple of cgi scripts my main purpose is to create a login page at this location

http://localhost//cgi-bin//login.pl


after login is verified i will go into say

http://localhost//cgi-bin//options.pl


i am creating a login so that if ppl change the login.pl to options.pl, they won't be able to access options.pl so they need to login but to do this i need to use CGI::Session i know now that initializing it is something like
CGI::Session->name("SID"); $session = new CGI::Session(); $cookie = $cgi->cookie( -name => $session->name, -value => $session->id ); print $cgi->header( -cookie=>$cookie );
however how do i know if i'm still in a session after my login page? so i'm not really sure about the codes and stuff, how do i do it to ensure i fullfill the above conditions? hope u all understand :)

Replies are listed 'Best First'.
Re: CGI::Session Usage Doubt
by trwww (Priest) on May 30, 2008 at 05:24 UTC

    Hello,

    You should check out CGI::Application.

    I put this example on the CGI::Application wiki a while ago. It abstracts the use of CGI::Application to a single use() statement:

    #!/usr/local/bin/perl use warnings; use strict; TestApp->new->run; package TestApp; use base qw|CGI::Application|; use CGI::Application::Plugin::AutoRunmode; use CGI::Application::Plugin::Session; sub default : StartRunmode { my $self = shift; my $output; # get current access count my $access_count = $self->session->param('access_count') || 0; # increment access count $access_count++; # store new access count in session $self->session->param(access_count => $access_count); # get CGI object out of application my $q = $self->query; #build output $output = $q->start_html( -title => 'access count application'); $output .= $q->div("accessed $access_count time(s) this session"); $output .= $q->div($q->a({-href => $q->self_url}, 'access again')); $output .= $q->end_html; # give output to app to send to user return( $output ); }

    This code simply increments a session counter each time the user accesses the page.

    In your code, you'd make a login and an options runmode (method). In the login runmode, put the user's database id in the session upon successful login. In the options runmode, check to see if the user's id is in the session (and valid!) before continuing.

    Enjoy

    trwww

Re: CGI::Session Usage Doubt
by scorpio17 (Canon) on May 30, 2008 at 14:10 UTC