Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

cgi::sessions can not create global session handle

by webg (Initiate)
on Feb 12, 2005 at 06:50 UTC ( [id://430371]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to use sessions in a website I'm building and I installed the CGI::Sessions module and got it to work fine, however when using it with the MySQL driver it will not save the session to the database unless the session handle is created locally within the subroutine. I'm trying to make the session accessible by everything in my program..works fine with the file driver however. FreeBSD 4.9, MySQL 12.22, Perl v5.8.3 Here is a test script to demonstrate what I'm talkin about.
#!/usr/bin/perl $dbn = "DBI:mysql:***"; $dbu = "***"; $dbp = "***"; use CGI; use DBI; use CGI::Session; my $cgi = new CGI; $dbh = DBI->connect($dbn, $dbu, $dbp, {RaiseError=>1,AutoCommit=>1}) o +r die ($DBI::errstr); &initsession; &addanothervalue; print $cgi->header; print "$sessionid<p> $session<p><p>"; sub addanothervalue { # -> comment out this line (to get working) $session->param('test',1); } sub initsession { #$session = new CGI::Session("driver:file",undef,{Directory=>'temp +'}); # -> add my to this line below (to get working) $session = new CGI::Session("driver:MySQL",undef,{Handle=>$dbh}); $sessionid = $session->id; $session->expire('+1h'); }
the way it is set right now, it doesn't work - it won't save the info to the database. however if you make the session handle local - it saves it to the database with a smile. thanks for any help, webg

Replies are listed 'Best First'.
Re: cgi::sessions can not create global session handle
by shenme (Priest) on Feb 12, 2005 at 10:06 UTC
    I've had varying 'experiences' with CGI::Session, and at some point added the following code to my CGI::Application teardown() routine:
    # Force CGI::Session flush now (rather than wait until object # destruction time when any errors/messages may be lost to us. $session->flush() or die "Unable to update session storage\n";
    Try again using a global for the $session object handle, but use the flush() method at the end of all your processing. You may solve the problem right there, or generate an error message that will lead you to a definite problem.
      excellent - I never would have tracked it down to that. Thanks very much for sharing your wisdom! webg

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://430371]
Approved by superfrink
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2024-03-28 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found