Well, I still can't muster the magic to get $Session properly[1]
viewable from ASP::XMLSubs or any other module, but here's how to get
Apache::Session working in a very basic way:

--- httpd.conf ---
PerlSetVar NoState 1

--- global.asa ---
BEGIN {
  @Apache::ASP::Objects = qw(Server Application Request Response);
}

use Apache::Session::MySQL;
use ASP::Session;

...

Script_OnStart {
    &start_session;
}

--- ASP/Session.pm ---
package ASP::Startup;

use Exporter;

use vars qw(@ISA @EXPORT);

@ISA    = qw(Exporter);
@EXPORT = qw(start_session $Session %session);

my $SESSION_EXPIRY = 60 * 20; # In seconds

our $Session;
our %session;

sub start_session {
    my %session_connect = (); # Apache::Session connection settings

    # Attempt to tie %session to the id from the cookie, or create a
    # new session.

    eval {
        tie(%session,
            'Apache::Session::MySQL',
            $main::Request->Cookies('_session_id'),
            \%session_connect);
    };

    if ($@) {
        die $@ unless $@ =~ /Object does not exist/;  # Re-throw

        # The session's been cleaned up, so create a new session.

        tie(%session,
            'Apache::Session::MySQL',
            undef,
            \%session_connect);
    }

    # If the session is more than $SESSION_EXPIRY seconds old, delete
    # it and start anew.

    if ($session{'_update_time'} &&
        time >= $session{'_update_time'} + $SESSION_EXPIRY) {
        tied(%session)->delete;
        tie(%session,
            'Apache::Session::MySQL',
            undef,
            \%session_connect);
    }

    $session{'_update_time'} = time;

    $Session = \%session;

    $main::Server->RegisterCleanup(sub { untie(%session); });

    $main::Response->Cookies('_session_id', $session{'_session_id'});
}

1;

Note that you'll still need something to purge old sessions from your
Apache::Session store. I created my session table like so:

CREATE TABLE `user_session` (
  `id` varchar(40) NOT NULL default '',
  `a_session` text,
  `a_update` timestamp NULL default NULL on update CURRENT_TIMESTAMP,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM

The timestamp lets me purge old sessions every hour.

[1] $Session /is/ accessible through
$Apache::ASP::Compiles::__ASP__var_www_document_rootx::Session, but
not main, leading to the very /very/ ugly hack:

my $session_obj = eval '$' . caller() . '::Session';
my $foo = $session_obj->{'FOO'};

No idea why that works.



--- In [EMAIL PROTECTED], "Eamon Daly" <[EMAIL PROTECTED]> wrote:
>
> I know this has been mentioned before on the list, but I'm having a
> heck of a time overriding $Session with Apache::Session. Josh
advises:
>
> "...I would use Apache::Session, which you can easily configure
> to use by setting 'PerlSetVar AllowSessionState Off' and then
setting
> $Session to the Apache::Session object created in Script_OnStart."
>
> http://groups.yahoo.com/group/apache-asp/message/1559
>
> In Script_OnStart, I have:
>
>     use vars qw(%session);
>     my $id = $Request->Cookies('_session_id') || undef;
>     tie %session, 'Apache::Session::MySQL', $id, \%session_connect;
>     $Session = \%session;
>     $Server->RegisterCleanup(sub { untie(%session); });
>     $Response->Cookies('_session_id', $session{'_session_id'});
>
> There's clearly a scoping issue there, though, because although
both %
> session and $Session are populated and read/writable within
> Script_OnStart, they aren't from actual pages. I can fix that with
> Script_OnParse:
>
>     my $code = $Server->{ScriptRef};
>     $$code = '<% $Session = \%session %>' . "\n$$code";
>
> which seems /awfully/ clumsy. Is that really TRWTDI?
>
> Secondly, I have an XMLSub that looks for certain information in
> $Session. Previously, I could refer to $main::Session, but that
> doesn't fly with the new overridden object. I don't know a way
around
> this one.
>
> Any help would be appreciated. Thanks!





---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to