neilwatson has asked for the wisdom of the Perl Monks concerning the following question:

Consider the following code. It seems that the internal_redirect prevents the cookie from being uploaded to the browser. How can I ensure that the cookie is sent to the browser before the redirect?
<%args> $uname => 'x' $passwd => 'x' $url </%args> <%perl> use warnings; use strict; use lib "/var/www/itiv/modules"; use Mapps::Session; use Mapps::Auth; ########################## # try to authenticate ########################## my ($auth, $uid) = Mapps::Auth::auth($uname, $passwd); if ($auth == 1){ my $s = Mapps::Session->new(); my $sid = $s->new_session($uid); #my $sid = $s->sid(); # set cookie Apache::Cookie->new( $r, name => 'session', value => $sid, path => '/', expires => '30m', )->bake; $r->internal_redirect($url); $m->auto_send_headers(0); $m->clear_buffer; $m->abort; # else no auth }else{ $r->internal_redirect("/login.html?msg=Wrong%20username%20or%20pas +sword"); $m->auto_send_headers(0); $m->clear_buffer; $m->abort; } </%perl>

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
•Re: Mason's internal_redirect and setting cookies
by merlyn (Sage) on Aug 05, 2004 at 14:50 UTC
    An internal redirect is, uh, internal!

    Since nothing is going to the browser, how do you expect a cookie to be sent to the browser? Last time I checked, most browsers were not omniscient.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Thanks for the kick. I need that :) Sorry to have bothered everyone.

      Neil Watson
      watson-wilson.ca

Re: Mason's internal_redirect and setting cookies
by Arunbear (Prior) on Aug 06, 2004 at 10:10 UTC
    A comment on style: if the URL's you are redirecting to are Mason components, you could streamline the code like this
    <%args> $uname => 'x' $passwd => 'x' $component </%args> <%perl> use warnings; use strict; use lib "/var/www/itiv/modules"; use Mapps::Session; use Mapps::Auth; ########################## # try to authenticate ########################## my ($auth, $uid) = Mapps::Auth::auth($uname, $passwd); if ($auth == 1){ my $s = Mapps::Session->new(); my $sid = $s->new_session($uid); #my $sid = $s->sid(); # set cookie Apache::Cookie->new( $r, name => 'session', value => $sid, path => '/', expires => '30m', )->bake; # invoke the requested component $m->comp($component); }else{ # no auth $m->comp("login.html", msg => "Wrong username or password"); } </%perl>
    Mason components (unlike PHP / ASP / JSP pages) are callable (like subroutines) so you can just call the component instead of doing a redirect.