Practical mod_perl / HTML Book /



previous page: A.4. Redirecting While Maintaining Environment Variables
  
page up: HTML Version of the book
  
next page: A.6. Sending Multiple Cookies with the mod_perl API

A.5. Handling Cookies


Unless you use a module such as CGI::Cookie or Apache::Cookie, you need to handle cookies yourself. Cookies are accessed via the $ENV{HTTP_COOKIE} environment variable. You can print the raw cookie string as $ENV{HTTP_COOKIE}. Here is a fairly well-known bit of code to take cookie values and put them into a hash:

sub get_cookies {
    # cookies are separated by a semicolon and a space, this will
    # split them and return a hash of cookies
    my @rawCookies = split /; /, $ENV{'HTTP_COOKIE'};
    my %cookies;

    foreach (@rawCookies){
        my($key, $val) = split /=/, $_;
        $cookies{$key} = $val;
    }

    return %cookies;
}

And here's a slimmer version:

sub get_cookies {
    map { split /=/, $_, 2 } split /; /, $ENV{'HTTP_COOKIE'};
}

 

Continue to:

  • prev: A.4. Redirecting While Maintaining Environment Variables
  • Table of Contents
  • next: A.6. Sending Multiple Cookies with the mod_perl API







TOP
previous page: A.4. Redirecting While Maintaining Environment Variables
  
page up: HTML Version of the book
  
next page: A.6. Sending Multiple Cookies with the mod_perl API


Menu

  • HTML Book
  • PDF Book
  • Download Code
  • Table of Contents
  • Errata
  • mod_perl2 User's Guide
  • Sitemap

Search


Add to Google




Creative Commons License


Written by
Eric Cholet (Logilune) and
Stas Bekman (StasoSphere & Free Books).


[ Privacy Policy | Terms of Use | About Us | Search ]

© 2007 StasoSphere.com