mod_perl logo perl icon
previous page: Writing mod_perl Handlers and Scriptspage up: User's guidenext page: Porting Apache:: Perl Modules from mod_perl 1.0 to 2.0

Cooking Recipes






Practical mod_perl

Practical mod_perl

By Stas Bekman, Eric Cholet
The mod_perl Developer's Cookbook

The mod_perl Developer's Cookbook

By Geoffrey Young, Paul Lindner, Randy Kobes
mod_perl Pocket Reference

mod_perl Pocket Reference

By Andrew Ford
Writing Apache Modules with Perl and C

Writing Apache Modules with Perl and C

By Lincoln Stein, Doug MacEachern
Embedding Perl in HTML with Mason

Embedding Perl in HTML with Mason

By Dave Rolsky, Ken Williams
mod_perl2 User's Guide

mod_perl2 User's Guide

By Stas Bekman, Jim Brandt


Table of Contents

Description

As the chapter's title implies, here you will find ready-to-go mod_perl 2.0 recipes.

If you know a useful recipe, not yet listed here, please post it to the mod_perl mailing list and we will add it here.



TOP

Sending Cookies in REDIRECT Response (ModPerl::Registry)

  use CGI::Cookie ();
  use Apache2::RequestRec ();
  use APR::Table ();
  
  use Apache2::Const -compile => qw(REDIRECT);
  
  my $location = "http://example.com/final_destination/";
  
  sub handler {
      my $r = shift;
  
      my $cookie = CGI::Cookie->new(-name  => 'mod_perl',
                                    -value => 'awesome');
  
      $r->err_headers_out->add('Set-Cookie' => $cookie);
      $r->headers_out->set(Location => $location);
      $r->status(Apache2::Const::REDIRECT);
  
      return Apache2::Const::REDIRECT;
  }
  1;


TOP

Sending Cookies in REDIRECT Response (handlers)

  use CGI::Cookie ();
  use Apache2::RequestRec ();
  use APR::Table ();
  
  use Apache2::Const -compile => qw(REDIRECT);
  
  my $location = "http://example.com/final_destination/";
  
  sub handler {
      my $r = shift;
  
      my $cookie = CGI::Cookie->new(-name  => 'mod_perl',
                                    -value => 'awesome');
  
      $r->err_headers_out->add('Set-Cookie' => $cookie);
      $r->headers_out->set(Location => $location);
  
      return Apache2::Const::REDIRECT;
  }
  1;

note that this example differs from the Registry example only in that it does not attempt to fiddle with $r->status() - ModPerl::Registry uses $r->status() as a hack, but handlers should never manipulate the status field in the request record.



TOP

Sending Cookies Using libapreq2

  use Apache2::Request ();
  use Apache2::RequestRec ();
  use Apache2::Const -compile => qw(OK);
  
  use APR::Table ();
  use APR::Request::Cookie ();
  
  sub handler {
      my $r = shift;
      my $req = $r->pool();
      
      my $cookie = APR::Request::Cookie->new($req, name => "foo", value => time(), path => '/cookie');
      
      $r->err_headers_out->add('Set-Cookie' => $cookie->as_string);
      
      $r->content_type("text/plain");
      $r->print("Testing....");
      
      return Apache2::Const::OK;
  }


TOP

Maintainers

Maintainer is the person(s) you should contact with updates, corrections and patches.



TOP

Authors

Only the major authors are listed above. For contributors see the Changes file.






TOP
previous page: Writing mod_perl Handlers and Scriptspage up: User's guidenext page: Porting Apache:: Perl Modules from mod_perl 1.0 to 2.0