You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Matthew <ma...@matthewboehm.com> on 2006/06/06 22:48:56 UTC

Sessions with mod_perl2

It could be that I just don't understand completely the nature of 
mod_perl, but here goes.

Reason I'm confused is because there exists CGI::Cookie for handling 
cookies in MP2, but there's also the libapreq library "for MP2".  It 
seems to me that the apreq lib is more "MP2 Native" than CGI would be. 
Am I wrong? Are they equally 'native' ?

With that in mind, I'm looking for some module, that I'm sure exists, 
that can manage sessions. I found Apache::Session but I couldn't tell if 
it was MP2 native.  Is there something else I should be using for 
sessions that is 'better' with MP2?

Thanks,
Matthew

Re: Sessions with mod_perl2

Posted by Tyler MacDonald <ty...@yi.org>.
Matthew <ma...@matthewboehm.com> wrote:
> It could be that I just don't understand completely the nature of 
> mod_perl, but here goes.
> 
> Reason I'm confused is because there exists CGI::Cookie for handling 
> cookies in MP2, but there's also the libapreq library "for MP2".  It 
> seems to me that the apreq lib is more "MP2 Native" than CGI would be. 
> Am I wrong? Are they equally 'native' ?
> 
> With that in mind, I'm looking for some module, that I'm sure exists, 
> that can manage sessions. I found Apache::Session but I couldn't tell if 
> it was MP2 native.  Is there something else I should be using for 
> sessions that is 'better' with MP2?

	CGI::Session works great with MP2.

	<shameless plug>Apache2::AUS works great with MP2 if you want a
full-fledged database backend that supports authentication, users, and
sessions as well. It's session component is based on CGI::Session, and it
allows you to configure everything from httpd.conf.</shameless plug>

		- Tyler


Re: Sessions with mod_perl2

Posted by Patrick Rutkowski <ru...@gmail.com>.
There is a plethora of different ways to handle sessions in perl  
driven websites. I'll throw in my 2 cents by describing how I've  
always liked to do it. My solution assumes that your using an SQL  
database for persistent data storage; but that obviously isn't a  
requirement, there are a lot of ways to manage persistent data. I  
also assume that your using mod_perl-2.

1) Use libapreq2's Apache2::Cookie to give the client a cookie such  
as "SID=57de5d0d-8315-0410-9c71-f10f4dd80f37" where  
57de5d0d-8315-0410-9c71-f10f4dd80f37 is a UUID generated by  
libapreq2's APR::UUID module. Three cheers for libapreq2! :-)  
Subsequently store the UUID into a database.
2) Check for a session cookie when a client connects to the site,  
again using libapreq2's Apache2::Cookie. If a session cookie exists,  
use a short and sweet SQL statement (via DBI of course) to fetch the  
session data from the back-end database and then a subsequent  
statement to store the fetched data in various variables for the rest  
site's code to use.
3) Have a single subroutine named, for example, store_session_data  
called with arguments such as store_session_data(UUID=>  
"57de5d0d-8315-0410-9c71-f10f4dd80f37" username => "rutski89",  
favorite_color => "blue", auto_login => "yes") which then does on the  
fly construction of an SQL statement to insert the session data into  
the database.

It's a step up from using CGI::Cookie in terms of the amount of work  
you have to do, but it's not too hard to implement. Personally, I  
enjoy the extra level of control over back-end storage.

Patrick R.

On Jun 6, 2006, at 4:48 PM, Matthew wrote:

> It could be that I just don't understand completely the nature of  
> mod_perl, but here goes.
>
> Reason I'm confused is because there exists CGI::Cookie for  
> handling cookies in MP2, but there's also the libapreq library "for  
> MP2".  It seems to me that the apreq lib is more "MP2 Native" than  
> CGI would be. Am I wrong? Are they equally 'native' ?
>
> With that in mind, I'm looking for some module, that I'm sure  
> exists, that can manage sessions. I found Apache::Session but I  
> couldn't tell if it was MP2 native.  Is there something else I  
> should be using for sessions that is 'better' with MP2?
>
> Thanks,
> Matthew


Re: Sessions with mod_perl2

Posted by Jonathan Vanasco <mo...@2xlp.com>.
On Jun 6, 2006, at 4:48 PM, Matthew wrote:

> It could be that I just don't understand completely the nature of  
> mod_perl, but here goes.

in a nutshell mod_perl lets you do at least these three things:
	compile cgi scripts into apache at runtime
	create mod_perl handlers that replace cgi scripts and do things on  
urls.  fun.
	let you script/interface with the apache server lifecycle

> Reason I'm confused is because there exists CGI::Cookie for  
> handling cookies in MP2, but there's also the libapreq library "for  
> MP2".  It seems to me that the apreq lib is more "MP2 Native" than  
> CGI would be. Am I wrong? Are they equally 'native' ?
libapreq is more for interfacing with apache get/post data,
the perl glue is native to either mp1 or mp2, but there is a c api  
for both

> With that in mind, I'm looking for some module, that I'm sure  
> exists, that can manage sessions. I found Apache::Session but I  
> couldn't tell if it was MP2 native.  Is there something else I  
> should be using for sessions that is 'better' with MP2?

i like apache session.  it works great under mp2

this is all cut from a custom session wrapper i have for handling  
sessions/cookies

checking for a cookie
	my 	$sessionID;
	eval
	{
		my 	$cookiejar 	= Apache2::Cookie::Jar->new( $self-> 
{'ApacheRequest'} );
		my 	@names 		= $cookiejar->cookies();
		if ( $cookiejar->cookies( $self->{'CookieDefaults'}{'names'} 
{'Session'} ) )
		{
			my 	%c_cookies = Apache2::Cookie->fetch( $self->{'ApacheRequest'} );
			my 	$c_value = $c_cookies{ $self->{'CookieDefaults'}{'names'} 
{'Session'} }->value;
			$sessionID = $self->__session__validate( $c_value );
		}
	};
	if ( $@ )
	{
		print STDERR "\nERROR - can not parse cookie";
	}
	return $sessionID;


setting a cookie
		my	$cookie = Apache2::Cookie->new	
		(
			$self->{'ApacheRequestRec'},
			-name 		=> $name   ,
			-value 		=> $value  ,
			-expires 	=> $expiry ,
			-secure 	=> $secure ,
		);
			$cookie->path( $self->{'CookieDefaults'}{'path'} );
			$cookie->domain( $domain );
		print STDERR "\n Baking cookie - $domain ";
		$self->{'ApacheRequestRec'}->err_headers_out->add('Set-Cookie' =>  
$cookie);