You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by gr...@gh-webinteractive.com on 2005/01/06 15:10:17 UTC
maintaining state between request cycles....
Hi,
in my application ( the Project XP ) I use mod_perl.
but I haven't yet found a good way of maintaining state between request
cycles, using hidden fields and cookies. Is there ( I hope ) a better way
to store the state of the client/server application between requests other
than hidden fields and cookies?
TIA
Greger
--
http://www.gh-webinteractive.com
Re: maintaining state between request cycles....
Posted by Sean Davis <sd...@mail.nih.gov>.
Greger,
This is some simple code I put in my modules that uses Apache::Session
and Apache::Cookie. $self->query is an Apache::Request object. The
first time this is called, it checks if a cookie (you could use a form
hidden field or a parameter in url to store the ID) exists and gets a
session_id from it. Then, a session is created with either the found
session_id or a new session_id is created. In both cases, the
session_id is stashed back into the cookie so that we can get it later.
The cookie expires in 1 hour, so after that, a new session will be
generated. Gurus, feel free to hack this up.
################################
sub session {
################################
# get session_id from cookie
my $self = shift;
return $self->{_session} if ($self->{_session});
my %session;
my %cookie = Apache::Cookie->new($self->query)->parse;
my $session_id = undef; # use undef here to get new session_id OR
if (exists $cookie{_session_id}) {
# get session_id from cookie if possible
$session_id=$cookie{_session_id}->value;
}
tie %session,'Apache::Session::MySQL',$session_id, {
DataSource =>
'dbi:mysql:database=nltissue;hostname=localhost',
UserName => 'xxxx',
Password => 'xxxx',
LockDataSource =>
'dbi:mysql:database=nltissue;hostname=localhost',
LockUserName => 'xxxx',
LockPassword => 'xxxx'} || die 'cannot do tie';
my $cookie = Apache::Cookie->new($self->query,
-name => '_session_id',
-value => $session{_session_id},
-expires=> '+1h',
) || die "cannot do cookie";
$cookie->bake;
$self->{_session} = \%session;
return \%session;
}
Sean
On Jan 6, 2005, at 9:20 AM, Michael Peters wrote:
> greger@gh-webinteractive.com wrote:
>> Hi,
>> in my application ( the Project XP ) I use mod_perl.
>> but I haven't yet found a good way of maintaining state between
>> request
>> cycles, using hidden fields and cookies. Is there ( I hope ) a better
>> way
>> to store the state of the client/server application between requests
>> other
>> than hidden fields and cookies?
>
> HTTP is a state-less environment. Once the server serves the request
> it forgets about the client. There is no way for the server to
> positively be able to identify the client on the clients next visit.
> The only way that state can be maintained is for the client to present
> some token to the server (either in a cookie, or hidden field, etc)
> telling the server who it is. I'm pretty sure that there is no way
> around this.
>
> Why does it bother you to use cookies or hidden fields?
>
> --
> Michael Peters
> Developer
> Plus Three, LP
Re: maintaining state between request cycles....
Posted by Michael Peters <mp...@plusthree.com>.
greger@gh-webinteractive.com wrote:
> Hi,
> in my application ( the Project XP ) I use mod_perl.
> but I haven't yet found a good way of maintaining state between request
> cycles, using hidden fields and cookies. Is there ( I hope ) a better way
> to store the state of the client/server application between requests other
> than hidden fields and cookies?
HTTP is a state-less environment. Once the server serves the request it
forgets about the client. There is no way for the server to positively
be able to identify the client on the clients next visit. The only way
that state can be maintained is for the client to present some token to
the server (either in a cookie, or hidden field, etc) telling the server
who it is. I'm pretty sure that there is no way around this.
Why does it bother you to use cookies or hidden fields?
--
Michael Peters
Developer
Plus Three, LP
Re: maintaining state between request cycles....
Posted by Issac Goldstand <ma...@beamartyr.net>.
http://search.cpan.org/dist/Apache-Session/Session.pm
----- Original Message -----
From: <gr...@gh-webinteractive.com>
To: "mod_perl" <mo...@perl.apache.org>
Sent: Thursday, January 06, 2005 4:10 PM
Subject: maintaining state between request cycles....
> Hi,
> in my application ( the Project XP ) I use mod_perl.
> but I haven't yet found a good way of maintaining state between request
> cycles, using hidden fields and cookies. Is there ( I hope ) a better way
> to store the state of the client/server application between requests other
> than hidden fields and cookies?
>
> TIA
> Greger
> --
> http://www.gh-webinteractive.com
>