[Catalyst] hostname

Jason Kohles email at jasonkohles.com
Fri Jan 11 13:48:24 GMT 2008


On Jan 11, 2008, at 5:29 AM, Carl Johnstone wrote:

>> cookie_domain => the_host()
>> in the MyApp config, but when I try to start the server it gives an  
>> error telling that I can't use the method "req" because $c is  
>> undefined.
>
> I'd be curious about why you wanted the cookie domain in the config  
> anyway!
>

I think the idea was something along the lines of...

MyApp->config->{ 'session' }->{ 'cookie_domain' } = MyApp->request- 
 >header->( 'host' );


> I presume you've got a bit of code like:
>
> $c->response->cookies->{'foo'} = { domain => $c- 
> >config('cookie_domain') };
>
> In which case why couldn't you just do
>
> $c->response->cookies->{'foo'} = { domain => $c->req->hostname };
>
>> But anyway, what I need is working because I can avoid setting a  
>> domain name for the cookie.
>
> Yeah exactly, setting the domain in the cookie to match the domain  
> requested is pretty pointless anyway as that's what browsers do by  
> default. About the only time you need to send a domain back is when  
> you want to set a cookie across all subdomains or similar. Eg:  
> { domain => '.example.com' }
>


In which case you could still do something in Root/auto if you wanted  
to modify it...

my $domain_re = join( '|', qw(
     mydomain.com
     myotherdomain.com
     foo.com
     example.com
) );

sub auto : Private {
     my ( $self, $c ) = @_;

     if ( $c->request->header( 'host' ) =~ /(\.(?:$domain_re))$/ ) {
         $c->config->{ 'session' }->{ 'cookie_domain' } = $1;
     }

     return 1;
}

-- 
Jason Kohles, RHCA RHCDS RHCE
email at jasonkohles.com - http://www.jasonkohles.com/
"A witty saying proves nothing."  -- Voltaire





More information about the Catalyst mailing list