I am completely baffled by cookies in mod_perl 2.0, I'm passing my Session Object a reference to either a CGI object or a CGI::Apache2::Wrapper object - my hope was that I could then treat them more or less the same in my Session object, however I simply cannot get the headers to include a cookie in mod_perl mode.

I originally tried using CGI::Cookie and that obviously didn't work I then tried using CGI::Apache2::Wrapper::Cookie->new to create a cookie object and then $self->cookie->bake() to make it set the header - I then used $self->cgi->send_http_header( $args{type} || '' ); to set the headers. The content type is XML so the headers seem to be getting set correctly - however no cookie...

I then tried using the CGI object's cookie method to create a cookie, which, as far as I can work out, should be syntatically identical for CGI and CGI::Apache2::Wrapper - however this then fails to set a cookie in either mode with either send_http_header or just cgi->header().

There seem to be a large number of ways of doing cookies in mod_perl2 and none of the examples seem to be complete (i.e. including how to then generate headers) or the ones that are complete just don't seem to work for me. I've now spent the last 2 hours trying various different styles out and have only succeeded in breaking cookies in the previously working non-mod_perl version.

Ideally I'd like code that didn't need to do $self->cgi->isa('CGI::Apache2::Wrapper') and then do something different for mod_perl as it seems to defeat the purpose!

My (relevant) code at the moment is:

sub set_client_side_cookie { my $self = shift; debug( "CGI is " . ref( $self->cgi ) ); if ( $self->cgi->isa('CGI::Apache2::Wrapper') ) { debug("Using mod_perl Cookie"); $self->{cookie} = CGI::Apache2::Wrapper::Cookie->new( $self->r, -name => $self->cookie_name, -value => $self->cookie_value, -expires => $self->cookie_expiry, -domain => $self->config->sys_opt('Cookie Domain') || $se +lf->server_name, ); } else { debug("Using CGI cookie"); $self->{cookie} = $self->cgi->cookie( -name => $self->cookie_name, -value => $self->cookie_value, -expires => $self->cookie_expiry, -domain => $self->config->sys_opt('Cookie Domain') || $se +lf->server_name, ); } } sub header { my $self = shift; my %args = @_; if ( $self->cgi->isa('CGI::Apache2::Wrapper') ) { $self->cookie->bake; $self->cgi->send_http_header( $args{type} || '' ); } else { return $self->cgi->header( -type => ( $args{type} || 'text/xml' ), -cookie => $self->cookie, ); } }

Where $self->cgi is a CGI or CGI::Apache2::Wrapper object and $self->r is an Apache::Request object.

My suspicion is that I'm not actually sending the headers and the content type is just co-incidentally being correctly detected...

Thanks in advance! Alex


In reply to mod_perl cookies and CGI::Apache2::Wrapper by amasidlover

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.