Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Apache::Session::MySql using a_session

by Anonymous Monk
on Jul 30, 2004 at 19:49 UTC ( #378788=perlquestion: print w/replies, xml ) Need Help??

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

Hi All,

Have searched through some of the postings on Apache::Sessions, but have not been able to find the anwser.

Here goes;

I need a way to store/pull static data in the 'sessions' table for Apache::Session::MySql. I was under the assumption that the 'a_session' field was created for just that purpose. Problem is, I have not idea how to get data into or out of that field. Can someone please give me some insight on how this would be accomplished.


Replies are listed 'Best First'.
Re: Apache::Session::MySql using a_session
by CountZero (Bishop) on Jul 30, 2004 at 21:56 UTC
    You don't mess with the database directly, but rather use the tie magic in the Apache::Session module, which is called by Apache::Session::MySql.

    It is as easy as declaring a hash, tieing this hash to an Apache::Session::MySql-object and the data in the hash are silently and transparently persisted into the database.

    Have a look at the docs in Apache::Session which are very clear.


    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Apache::Session::MySql using a_session
by Your Mother (Archbishop) on Jul 31, 2004 at 07:34 UTC

    You can look at all the stuff that's already in an established session DB with something like this:

    use Data::Dumper; $Data::Dumper::Terse = 1; $Data::Dumper::Indent = 1; use DBI; use Apache::Session::MySQL; my $dbh = DBI->connect('DBI:mysql:apache_session' . ':' . 'localhost' . ';mysql_read_default_file=/path/to/.my.cnf', undef, undef, ); ( my $sth = $dbh->prepare('SELECT id FROM sessions') )->execute; while ( my $id = $sth->fetchrow_array ) { my %session; tie %session, 'Apache::Session::MySQL', $id, { Handle => $dbh, LockHandle => $dbh }; print Dumper \%session; # you could edit or do whatever to %session here and get the # data back with the same $id later } $sth->finish; $dbh->disconnect;

    For creating and manipulating the sessions, you should use the module itself, as CountZero suggested. It is in control of the "a_session" column's data (just a text field) and its serialization/retrieval, and you should almost certainly leave it that way.

Re: Apache::Session::MySql using a_session
by Anonymous Monk on Aug 02, 2004 at 02:19 UTC

    Correct, I would not want to change the table directly (even though that would be easier at this point). I try setting values like the example shows (ie. visa_number). But it never makes it back to the database table. Here is some example code from my script:

    ... Other code ....

    my %session;
    tie %session, 'Apache::Session::MySQL', $cookie, {
    DataSource => 'dbi:mysql:sessions', #these arguments are
    UserName => '*******', #required when using
    Password => '*******',
    LockDataSource => 'dbi:mysql:sessions',
    LockUserName => '*******',
    LockPassword => '*******'


    ... Other code ....

    untie %session;
    undef %session;

    Again, nothing seems to make it back into the table. =(

    Any Ideas?

      While I am sure this problem has been solved since the original post was submitted I figured I would make a suggestion in regard to viewing session.a_session data in MySQL. What is stored in the session.a_session isn't visible by "SELECT a_session FROM sessions WHERE id=?" ... Apache::Session serializes the data before inserting it.

      Once your session has been created and your additional session-data has been assigned to your tied hash ($session{foo} = "bar";), dump %session on a SUBSEQUENT VISIT and view the available data. If all works as planned, you will see foo => "bar" along with your session id.

      Another (down & dirty) way to verify that session.a_session is indeed receiving the specified data is to "less session.MYD" (MySQL data file) on your web server ... assuming you have access. The output may be hard to understand but you should be able to recognize a session variable here or there.
      Just a thought.

      author => jeyroz

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (1)
As of 2023-10-02 17:29 GMT
Find Nodes?
    Voting Booth?

    No recent polls found