Hello fellow monks... I managed to get CGI::Session working with my previous project (missing a $session->close). Anyways, I'm trying to implement CGI::Session in a new project with unexpected results. After creating the $session object, it should assign a value ($session->id) to $sid if it's not already defined from the cookie. However, the $sid is not being assigned. The only thing I can figure is that I don't have proper system permissions, because the session db and lockfile aren't being created.
Here is the relevant code. During the initial connection, the if/elsif/else block will default to &login_form, where it prints the default login screen and (for debugging purposes) the $sid. Unfortunately, I'm getting back the dreaded "CGI=HASH(0x814c738)" answer instead of a real session id.
On another issue, you might notice that I've called for no strict vars. My reason is that I'm designing the script as one main "handler" and various sub scripts. Unfortunately, I'm not very experienced (read: none) with creating packages or modules, so I've simply "require"-d the necessary sub scripts. If I try to run the main script with full strict-ness, I get all kinds of scope errors from the sub scripts. How can I make these global variables truly global?
Thanks as always...
-fuzzyping
#!/usr/bin/perl
use strict;
no strict 'vars';
use CGI qw(:standard);
use CGI::Session::DB_File;
use Crypt::PasswdMD5;
use DBI;
$| = 1;
require "/var/www/cgi-bin/lunchforum/includes/errors.pl";
require "/var/www/cgi-bin/lunchforum/includes/login.pl";
require "/var/www/cgi-bin/lunchforum/includes/main_forum.pl";
require "/var/www/cgi-bin/lunchforum/includes/newaccount.pl";
$title = "Digex Lunch Forum";
$dbpath = "/var/www/cgi-bin/nobody/lunchforum";
$database = "dlforum";
$server = "localhost";
$dbuser = "dlfuser";
$dbpasswd = "m1ck3yd335";
$cgi = CGI->new;
$sid = $cgi=>cookie('hungry') || undef;
$session = new CGI::Session::DB_File($sid, {Filename=>"$dbpath/lunchfo
+rum.db", LockDirectory=>"$dbpath/"});
$sid ||=$session->id;
$cookie = $cgi->cookie(-name=>'hungry', -value=>$sid, -expires=>'+30m'
+);
$dbh = DBI->connect("DBI:mysql:$database:$server","$dbuser","$dbpasswd
+");
if (param('newaccount')) {
&newaccount_form
} elsif (param('newlogin')) {
$username = &login('newuser');
&newaccount($username);
} elsif (param('oldlogin')) {
$username = &login;
&main_handler($username)
} elsif (!(param('logged'))) {
&login_form
} else {
$username = ($session->param('username') || "duh");
&main_handler($username)
}
$dbh->disconnect;
sub main_handler {
$username = shift;
&page_navbar($username);
&page_header($username);
&page_body($username);
}
sub print_header {
print $cgi->header(
-type=>'text/html',
-cookie=>$cookie,
-start_html=>$title)
}
and the login sub...
sub login_form {
&print_header;
print "<center>",
$cgi->start_form,
$cgi->h1($title),
$sid,br,
"Welcome to the Digex Lunch Forum.",br,
"Please login below or check \"New Account\"",br,
"to sign up for our group and receive your premier ben
+efits.",br,br,
"<table><tr><td>Username </td><td>",
$cgi->textfield('username'),"</td></tr><tr><td>Passwor
+d </td><td>",
$cgi->password_field('secret'),"</td></tr></table>",br
+,
$cgi->checkbox_group(-name=>'newaccount', -value=>'New
+ Account'),br,br,
$cgi->submit(-name=>'oldlogin', -value=>'Next'), " ",
$cgi->defaults('Clear'),"</center>",
$cgi->end_form,
$cgi->end_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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.