Re: maintaining state between request cycles....

[prev] [thread] [next] [Date index for 2005/01/06]

From: Sean Davis
Subject: Re: maintaining state between request cycles....
Date: 14:35 on 06 Jan 2005
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@xxxxxxxxxxxxxxxxx.xxx 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....
Issac Goldstand 14:11 on 06 Jan 2005

Re: maintaining state between request cycles....
Michael Peters 14:20 on 06 Jan 2005

Re: maintaining state between request cycles....
Sean Davis 14:35 on 06 Jan 2005

Generated at 11:30 on 23 Feb 2005 by mariachi v0.52