Hi,

I am trying to use CGI::Session 4.42 on a Redhat Enterprise system running Apache 2. I also have the default redhat MySQL build for my redhat version.

I am just starting out with CHI::Session and I must be doing something wrong... but I can not figure it out.

I have tried to use ideas from the CGI::Session tutorial and I have the following for a login function:

NOTES: "$dbh" is an active database handle. It is defined and created elsewhere in the code.
"$CGI" is an active CGI.pm instance. It is defined and created elsewhere in the code.
"$CONFIG" is a hash that holds configuration info that is pulled in from an external onfig file.

The following executes without error but no records are stored in the database. A sessionID is acquired from CGI::Session and a cookie is created that looks correct but, again, no session data is stored in the "sessions" table.

All ideas are welcome. I am probably not doing something fundamental.

sub login { my ($dbh) = @_; my ($email) = $dbh->quote($CGI->param('email') || undef); my ($password) = $dbh->quote($CGI->param('password') || undef); my ($loginSessionID) = $CGI->param('loginSessionID') || undef; eval { # # email should be an RFC compliant email address. # my $tmp = <<EOF; SELECT id FROM users WHERE LOWER(email) = LOWER($email) AND password = $password EOF my $sth = &return_query($dbh, $tmp); my @row = $sth->fetchrow_array; $result{userID} = undef; if (@row) { $result{userID} = $row[0]; CGI::Session->name( $CONFIG->{userSession}{cookies}{LOGIN}{name} || "CGISESSID"); $SESSION = CGI::Session->load("driver:MySQL", $CGI || $loginSessionID, { DataSource => join(':', $CONFIG->{mysql}{DSN}, $CONFIG->{mysql}{Collection}), TableName => 'sessions', IdColName => 'id', DataColName => 'data', Handle => $dbh, }) || die CGI::Session->errstr() . "\n"; if ($SESSION->is_expired) { # # If we are picking up an old session see if it is # already expired. If it is expired delete it from # the store and flush the session data. # $SESSION->delete(); $SESSION->flush(); } if ($SESSION->is_empty) { # # This "new" is CGI::Session magic. THe session # definition in the "load" above is remembered and # will be re-applied here. # $SESSION = CGI::Session->new(); } $result{loginSessionID} = $SESSION->id(); $SESSION->param('userID', $result{userID}); $SESSION->expire( $CONFIG->{userSession}{cookies}{LOGIN}{expire} | +| "+30m"); $COOKIES{loginSession} = $CGI->cookie( -name => $CONFIG->{userSession}{cookies}{LOGIN}{nam +e}, -value => $SESSION->id(), -expires => $CONFIG->{userSession}{cookies}{LOGIN}{expire} | +| "+30m", -path => $CONFIG->{userSession}{cookies}{LOGIN}{pat +h} || "/", -domain => $CONFIG->{userSession}{cookies}{LOGIN}{domain} | +| "", -secure => $CONFIG->{userSession}{cookies}{LOGIN}{secure} | +| 0, ); $SESSION->flush(); } }; if ($@) { push(@{ $result{error} }, $@); } return encode_json(\%result); }

In reply to CGI::Session not "storing" anything by seven.reeds

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.