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

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; }

Replies are listed 'Best First'.
Re: More problems with CGI::Session
by Juerd (Abbot) on Mar 14, 2002 at 22:58 UTC

    $sid = $cgi=>cookie('hungry') || undef;

    Something tells me that => should have been ->.

    the CGI=HASH(0xFOO) means the thing you get back is an object from the CGI package (this is your $cgi, and it will probably change if you change the comma arrow to a method arrow).

    HTH

    Update - erh... I shouldn't be typing :wq in mozilla :)

    U28geW91IGNhbiBhbGwgcm90MTMgY
    W5kIHBhY2soKS4gQnV0IGRvIHlvdS
    ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
    geW91IHNlZSBpdD8gIC0tIEp1ZXJk
    

      That was it! I don't know how you saw that... I though I had good vision. :-P

      Thanks!!!

      -fuzzyping UPDATE: Can anyone answer my 2nd question?

        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?

        Well... if I'm not mistaken you can make any variable a (package) global variable with the use vars qw($some @vars); pragma. If I'm mistaken, please correct me.

        However, I'd suggest that you take a moment and read a bit about packages and namespaces. You will be able to resolve all those scope issues that use strict; is complaining about by changing from require-ing an external script that contains your subs, to use-ing or require-ing a perl module (*.pm) file. This allows you to manage your variables in nice, neat namespaces . Sure, it's a little more work (and incidentally it took me many readings of the documentation ;) initially but the payoff is huge.

        Good luck fuzzyping!!!!

        ..Guv

Re: More problems with CGI::Session
by oubiwann (Sexton) on Mar 14, 2002 at 22:57 UTC
    What happens when you dereference the hash? Can you iterate through it and get good/expected values?
      There is no hash, that's just the error I'm getting.

      -fuzzyping
        Actually, there is a hash. Would perl lie to you? May the heavens prevent such a day... Okay, so I'm sick. I like to peek under the skirts of perl (holy blasphemy? erotic mysticism?) I d'led your code and parsed the hash. For ease of eye, I used Data::Dumper:
        $VAR1 = bless( { '.header_printed' => '1', '.charset' => 'ISO-8859-1', '.parameters' => [], '.fieldnames' => {} }, 'CGI' );
        To me this says that somehow $sid became a reference to the object... as Juerd said below. So looking at the type that Juerd found, does anyone know what really happened here? $sid was assigned the value of $cgi, right? But what happened internally when fuzz tried to use the '=>' operator on it? Did it just say "no way, man... I don't know how to do that - I'm just giving you my data"?
Re: More problems with CGI::Session
by Anonymous Monk on Sep 08, 2002 at 03:38 UTC
    Hi folks. I encourage you guys to checkout the latest release of the library and the documentation. Comes with a sessioncook manual as well, might find it useful. All of the known bugs have been resolved.