CGI::Cookie man page on AIX

Man page or keyword search:  
man Server   4752 pages
apropos Keyword Search (all sections)
Output format
AIX logo
[printable version]

CGI::Cookie(3)	       Perl Programmers Reference Guide		CGI::Cookie(3)

       CGI::Cookie - Interface to Netscape Cookies

	   use CGI qw/:standard/;
	   use CGI::Cookie;

	   # Create new cookies and send them
	   $cookie1 = new CGI::Cookie(-name=>'ID',-value=>123456);
	   $cookie2 = new CGI::Cookie(-name=>'preferences',
				      -value=>{ font => Helvetica,
						size => 12 }
	   print header(-cookie=>[$cookie1,$cookie2]);

	   # fetch existing cookies
	   %cookies = fetch CGI::Cookie;
	   $id = $cookies{'ID'}->value;

	   # create cookies returned from an external source
	   %cookies = parse CGI::Cookie($ENV{COOKIE});

       CGI::Cookie is an interface to Netscape (HTTP/1.1) cookies, an innova‐
       tion that allows Web servers to store persistent information on the
       browser's side of the connection.  Although CGI::Cookie is intended to
       be used in conjunction with (and is in fact used by it inter‐
       nally), you can use this module independently.

       For full information on cookies see

       CGI::Cookie is object oriented.	Each cookie object has a name and a
       value.  The name is any scalar value.  The value is any scalar or array
       value (associative arrays are also allowed).  Cookies also have several
       optional attributes, including:

       1. expiration date
	   The expiration date tells the browser how long to hang on to the
	   cookie.  If the cookie specifies an expiration date in the future,
	   the browser will store the cookie information in a disk file and
	   return it to the server every time the user reconnects (until the
	   expiration date is reached).	 If the cookie species an expiration
	   date in the past, the browser will remove the cookie from the disk
	   file.  If the expiration date is not specified, the cookie will
	   persist only until the user quits the browser.

       2. domain
	   This is a partial or complete domain name for which the cookie is
	   valid.  The browser will return the cookie to any host that matches
	   the partial domain name.  For example, if you specify a domain name
	   of "", then Netscape will return the cookie to Web
	   servers running on any of the machines "",
	   "", "", etc.	Domain names
	   must contain at least two periods to prevent attempts to match on
	   top level domains like ".edu".  If no domain is specified, then the
	   browser will only return the cookie to servers on the host the
	   cookie originated from.

       3. path
	   If you provide a cookie path attribute, the browser will check it
	   against your script's URL before returning the cookie.  For exam‐
	   ple, if you specify the path "/cgi-bin", then the cookie will be
	   returned to each of the scripts "/cgi-bin/",
	   "/cgi-bin/", and "/cgi-bin/customer_service/",
	   but not to the script "/cgi-private/".	By default,
	   the path is set to "/", so that all scripts at your site will
	   receive the cookie.

       4. secure flag
	   If the "secure" attribute is set, the cookie will only be sent to
	   your script if the CGI request is occurring on a secure channel,
	   such as SSL.

       4. httponly flag
	   If the "httponly" attribute is set, the cookie will only be acces‐
	   sible through HTTP Requests. This cookie will be inaccessible via
	   JavaScript (to prevent XSS attacks).

	   But, currently this feature only used and recognised by MS Internet
	   Explorer 6 Service Pack 1 and later.

	   See this URL for more information:


       Creating New Cookies

	       my $c = new CGI::Cookie(-name	=>  'foo',
				    -value   =>	 'bar',
				    -expires =>	 '+3M',
				    -domain  =>	 '',
				    -path    =>	 '/cgi-bin/database',
				    -secure  =>	 1

       Create cookies from scratch with the new method.	 The -name and -value
       parameters are required.	 The name must be a scalar value.  The value
       can be a scalar, an array reference, or a hash reference.  (At some
       point in the future cookies will support one of the Perl object serial‐
       ization protocols for full generality).

       -expires accepts any of the relative or absolute date formats recog‐
       nized by, for example "+3M" for three months in the future.  See's documentation for details.

       -domain points to a domain name or to a fully qualified host name.  If
       not specified, the cookie will be returned only to the Web server that
       created it.

       -path points to a partial URL on the current server.  The cookie will
       be returned to all URLs beginning with the specified path.  If not
       specified, it defaults to '/', which returns the cookie to all pages at
       your site.

       -secure if set to a true value instructs the browser to return the
       cookie only when a cryptographic protocol is in use.

       -httponly if set to a true value, the cookie will not be accessible via

       For compatibility with Apache::Cookie, you may optionally pass in a
       mod_perl request object as the first argument to "new()". It will sim‐
       ply be ignored:

	 my $c = new CGI::Cookie($r,
				 -name	  =>  'foo',
				 -value	  =>  ['bar','baz']);

       Sending the Cookie to the Browser

       The simplest way to send a cookie to the browser is by calling the
       bake() method:


       Under mod_perl, pass in an Apache request object:


       If you want to set the cookie yourself, Within a CGI script you can
       send a cookie to the browser by creating one or more Set-Cookie: fields
       in the HTTP header.  Here is a typical sequence:

	 my $c = new CGI::Cookie(-name	  =>  'foo',
				 -value	  =>  ['bar','baz'],
				 -expires =>  '+3M');

	 print "Set-Cookie: $c\n";
	 print "Content-Type: text/html\n\n";

       To send more than one cookie, create several Set-Cookie: fields.

       If you are using, you send cookies by providing a -cookie argu‐
       ment to the header() method:

	 print header(-cookie=>$c);

       Mod_perl users can set cookies using the request object's header_out()

	 $r->headers_out->set('Set-Cookie' => $c);

       Internally, Cookie overloads the "" operator to call its as_string()
       method when incorporated into the HTTP header.  as_string() turns the
       Cookie's internal representation into an RFC-compliant text representa‐
       tion.  You may call as_string() yourself if you prefer:

	 print "Set-Cookie: ",$c->as_string,"\n";

       Recovering Previous Cookies

	       %cookies = fetch CGI::Cookie;

       fetch returns an associative array consisting of all cookies returned
       by the browser.	The keys of the array are the cookie names.  You can
       iterate through the cookies this way:

	       %cookies = fetch CGI::Cookie;
	       foreach (keys %cookies) {

       In a scalar context, fetch() returns a hash reference, which may be
       more efficient if you are manipulating multiple cookies. uses the URL escaping methods to save and restore reserved char‐
       acters in its cookies.  If you are trying to retrieve a cookie set by a
       foreign server, this escaping method may trip you up.  Use raw_fetch()
       instead, which has the same semantics as fetch(), but performs no

       You may also retrieve cookies that were stored in some external form
       using the parse() class method:

	      $COOKIES = `cat /some/path/Cookie_stash`;
	      %cookies = parse CGI::Cookie($COOKIES);

       If you are in a mod_perl environment, you can save some overhead by
       passing the request object to fetch() like this:


       Manipulating Cookies

       Cookie objects have a series of accessor methods to get and set cookie
       attributes.  Each accessor has a similar syntax.	 Called without argu‐
       ments, the accessor returns the current value of the attribute.	Called
       with an argument, the accessor changes the attribute and returns its
       new value.

	   Get or set the cookie's name.  Example:

		   $name = $c->name;
		   $new_name = $c->name('fred');

	   Get or set the cookie's value.  Example:

		   $value = $c->value;
		   @new_value = $c->value(['a','b','c','d']);

	   value() is context sensitive.  In a list context it will return the
	   current value of the cookie as an array.  In a scalar context it
	   will return the first value of a multivalued cookie.

	   Get or set the cookie's domain.

	   Get or set the cookie's path.

	   Get or set the cookie's expiration time.

       Copyright 1997-1998, Lincoln D. Stein.  All rights reserved.

       This library is free software; you can redistribute it and/or modify it
       under the same terms as Perl itself.

       Address bug reports and comments to:

       This section intentionally left blank.

       CGI::Carp, CGI

perl v5.8.8			  2008-09-19			CGI::Cookie(3)
                             _         _         _ 
                            | |       | |       | |     
                            | |       | |       | |     
                         __ | | __ __ | | __ __ | | __  
                         \ \| |/ / \ \| |/ / \ \| |/ /  
                          \ \ / /   \ \ / /   \ \ / /   
                           \   /     \   /     \   /    
                            \_/       \_/       \_/ 
More information is available in HTML format for server AIX

List of man pages available for AIX

Copyright (c) for man pages and the logo by the respective OS vendor.

For those who want to learn more, the polarhome community provides shell access and support.

[legal] [privacy] [GNU] [policy] [cookies] [netiquette] [sponsors] [FAQ]
Polarhome, production since 1999.
Member of Polarhome portal.
Based on Fawad Halim's script.
Vote for polarhome
Free Shell Accounts :: the biggest list on the net