You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Jason Jolly <ja...@hotmail.com> on 2003/04/01 02:11:21 UTC

cookie setting/retieval

I'm currently in the process of writing a wrapper for a site using Apache::Session and mod_perl.  I was having great success using the following code to either validate a cookie which was already present or set a new cookie if necessary:

        my $r = Apache->request;
        my $cookie = $r->header_in('Cookie');
        my $cgi = new CGI;

        $cookie =~ s/SESSION_ID=(\w*)/$1/;

        # create the session from the cookie or create a new one
        my %session;
        tie %session, 'Apache::Session::File', $cookie, {
                Directory     => '/tmp/sessions',
        };

        my $session_cookie = "SESSION_ID=$session{_session_id};";
        $r->header_out("Set-Cookie" => $session_cookie);

Now...this works like a charm, passing the cookie and reading the session....as long as all of the pages are on the same level of the tree...i.e. :

        http://www.yoursite.com/perl/hithere.cgi
        http://www.yoursite.com/perl/ilovecookies.cgi
        http://www.yoursite.com/perl/monkey.cgi

However....as soon as you move to anything below that:

        http://www.yoursite.com/perl/cookies/bananna.cgi
        http://www.yoursite.com/perl/cookies/arenolongercool.cgi

I receive the following error (from the error_log):

        "Died at /usr/local/lib/perl5/site_perl/5.8.0/Apache/Session/Generate/MD5.pm line 40"

By using Mozilla's cookie manager I see that the path attribute of the cookie set in the browser is changed from:

        "/perl/"
to:
    
        "/perl/cookies/"

...but I can't seem to figure out why Apache::Session isn't using the combination of the SESSION_ID and the directory stated in the tie() (/tmp/sessions) to retrieve the cookie...the SESSION_ID hasn't changed?

Thnx much!

~j

Re: cookie setting/retieval

Posted by Jason Jolly <ja...@hotmail.com>.
Enrico,

If I do migrate to Apache::SessionManager will this problem disappear?  I
started looking into the transformation and it appears as if it will be a
good bit of work to accomplish the entire cutover and I just want to make
for sure that it's not something I'd have to build into my app anyway.

The reason I ask is because I was having some trouble with the
configuration?  After putting the following into the httpd.conf file

## BEGIN CONFIGURATION

PerlRequire             /usr/local/apache/conf/startup.pl
PerlWarn                On
PerlFreshRestart      On
PerlModule Apache::SessionManager
PerlTransHandler Apache::SessionManager
Alias /perl /usr/local/apache/cgi-bin

<Location /perl>
        SetHandler              perl-script
        PerlSendHeader          On
        PerlHandler             Apache::Registry
        PerlSetVar SessionManagerTracking On
        PerlSetVar SessionManagerExpire 120
        PerlSetVar SessionManagerInactivity 900
        PerlSetVar SessionManagerName SESSION_ID
        PerlSetVar SessionManagerStore File
        PerlSetVar SessionManagerStoreArgs "Directory     => /tmp/sessions"
        PerlSetVar SessionManagerDebug 0
        Options                 ExecCGI
        allow from all
</Location>

and using the code........

## BEGIN CODE
    sub validate {
        my $r=shift;
        my $cgi=new CGI;

        my $session = Apache::SessionManager::get_session($r);

        if (($$session{logged_in} eq "Y")){
                return $session;
        }else{
                print $cgi->redirect('http://' . $ENV{HTTP_HOST} .
'/perl/sess.cgi');
                return $session;
        }
    }

I always get the error:

    Can't locate object method "pnotes" via package "<package_name>" at
/usr/local/lib/perl5/site_perl/5.8.0/Apache/SessionManager.pm line 202,
<TheFile> line 28.


I'm sure I'm going about something wrong, but I seem to have hit a
roadblock.....do I need to write my own handler for this?

thnx,

~j

----- Original Message -----
From: "Enrico Sorcinelli" <e....@pisa.iol.it>
To: "Jason Jolly" <ja...@hotmail.com>
Cc: <mo...@perl.apache.org>
Sent: Tuesday, April 01, 2003 1:52 AM
Subject: Re: cookie setting/retieval


> On Mon, 31 Mar 2003 18:11:21 -0600
> "Jason Jolly" <ja...@hotmail.com> wrote:
>
> > I'm currently in the process of writing a wrapper for a site using
Apache::Session and mod_perl.  I was having great success using the
following code to either validate a cookie which was already present or set
a new cookie if necessary:
>
> Hi Jason,
> don't reinvent the wheel! ;-)
> There's already an Apache::Session wrapper on CPAN called
Apache::SessionManager.
> Apache::SessionManager is a mod_perl module that helps session management
of a
> web application. It creates a session object and makes it available to all
> other handlers transparenlty. Also session expiration policies are
available.
>
> http://cpan.org/modules/by-module/Apache/Apache-SessionManager-0.04.tar.gz
>
> See perldoc Apache::SessionManager for more infos.
>
> by
>
> - Enrico
>

Re: cookie setting/retieval

Posted by Enrico Sorcinelli <e....@pisa.iol.it>.
On Mon, 31 Mar 2003 18:11:21 -0600
"Jason Jolly" <ja...@hotmail.com> wrote:

> I'm currently in the process of writing a wrapper for a site using Apache::Session and mod_perl.  I was having great success using the following code to either validate a cookie which was already present or set a new cookie if necessary:

Hi Jason, 
don't reinvent the wheel! ;-)
There's already an Apache::Session wrapper on CPAN called Apache::SessionManager. 
Apache::SessionManager is a mod_perl module that helps session management of a 
web application. It creates a session object and makes it available to all 
other handlers transparenlty. Also session expiration policies are available.

http://cpan.org/modules/by-module/Apache/Apache-SessionManager-0.04.tar.gz

See perldoc Apache::SessionManager for more infos.

by

	- Enrico