Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: ModPerl: ASP

Apache::ASP and Apache::Session

 

 

ModPerl asp RSS feed   Index | Next | Previous | View Threaded


eamon at eamondaly

Jan 17, 2006, 12:28 PM

Post #1 of 3 (3390 views)
Permalink
Apache::ASP and Apache::Session

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: asp-unsubscribe [at] perl
For additional commands, e-mail: asp-help [at] perl


eamon at eamondaly

Jan 21, 2006, 11:30 AM

Post #2 of 3 (3068 views)
Permalink
Re: Apache::ASP and Apache::Session [In reply to]

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 apache-asp [at] yahoogroups, "Eamon Daly" <eamon [at] e> 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: asp-unsubscribe [at] perl
For additional commands, e-mail: asp-help [at] perl


eamon at eamondaly

Jan 21, 2006, 3:12 PM

Post #3 of 3 (3102 views)
Permalink
Re: Apache::ASP and Apache::Session [In reply to]

I could have /sworn/ I'd tried that! In any case, that works
great. Thanks!

____________________________________________________________
Eamon Daly



----- Original Message -----
From: "John Drago" <john.drago [at] data393>
To: "Eamon Daly" <eamon [at] eamondaly>; <asp [at] perl>
Sent: Saturday, January 21, 2006 4:05 PM
Subject: RE: Apache::ASP and Apache::Session


Eamon - in your ASP::Startup package, make sure you do this:

$main::Session = $Session;

That way you will be able to access $main::Session from your XMLSubs.



---------------------------------------------------------------------
To unsubscribe, e-mail: asp-unsubscribe [at] perl
For additional commands, e-mail: asp-help [at] perl

ModPerl asp RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.