Here's some sample scripts so it's clearer what I'm attempting to do & how everything works:
Login script:
#!c:/apache/perl/bin/perl.exe -wT
use CGI;
use strict;
use CGI::Session;
use HTML::Template;
use MyAppCommon;
my $foo = new CGI;
my $alias = $foo->param('alias');
my $userID = 123; #normally would be pulled from DB, but keeping the c
+ode as simple as possible for testing purposes
my $empty;
my $session = new CGI::Session(undef, $empty, {Directory=>"c:/apache/s
+essions"});
$session->expire('+18h');
$session->param("id", $userID);
$session->param("alias", $alias);
my $cookie = $foo->cookie(-name => 'main',
-value => $session->id,
-expires => '+18h',
-path => '/');
print $foo->header(-cookie => $cookie);
MyAppCommon::topPublic();
my $template=HTML::Template->new(filename=>"c:/apache/templates/main/l
+ogin.tmpl");
$template->param(ALIAS => $alias);
print $template->output;
MyAppCommon::bottomPublic($alias);
One of the many pages that contain very similar coding (a page that simply wants to check if the user is logged in or not, and does NOT want to log them in if they're not... ie. does NOT create a session if they're not logged in... it only wants to check if they are & retrieve relevant session variables if they are logged in:
#!c:/apache/perl/bin/perl.exe -wT
use CGI;
use strict;
use CGI::Session;
use HTML::Template;
use MyAppCommon;
my $foo = new CGI;
print $foo->header();
my $sid = cookie('main') || undef;
my @sessionInfo;
my $session = new CGI::Session(undef, $sid, {Directory=>'c:/apache/ses
+sions'});
if ( $session->is_new() ) {
$session->delete();
}
MyAppCommon::topPublic();
my $template=HTML::Template->new(filename=>"c:/apache/templates/main/h
+ome.tmpl");
print $template->output;
MyAppCommon::bottomPublic($alias, $userAccess, $membership);
IF I comment out the following code of the above script:
my $sid = cookie('main') || undef;
my @sessionInfo;
my $session = new CGI::Session(undef, $sid, {Directory=>'c:/apache/ses
+sions'});
if ( $session->is_new() ) {
$session->delete();
}
and replace it with:
&test;
sub test {
my $sid = cookie('main') || undef;
my @sessionInfo;
my $session = new CGI::Session(undef, $sid, {Directory=>'c:/apache
+/sessions'});
if ( $session->is_new() ) {
$session->delete();
}
return 1;
}
I get the error I mentioned.
" on every page, you check whether there's a session logged in or not
# depending whether there is one, you do whatever you need to do"
This is exactly what I want to do. Using the sessions ID, I want to determine if one exists with that session ID and to *NOT* create one if it doesn't exist.
|