Retrieving and Setting HTTP Cookies

This section provides a tutorial example on how to retrieve cookies from the HTTP request header and set cookies to the HTTP response header. Cookie objects are created with the CGI::Cookie class.

Here is a simple example Perl CGI script:, to test the CGI::Cookie class:

#- Copyright (c) All Rights Reserved.

   use CGI;
   use CGI::Cookie;

   $query = CGI->new();

#- Retrieving old cookies from the incoming request
   $text = "";
   @cookies = $query->cookie();
   $text .= (scalar @cookies).' Cookies received from request:'."\n";

   $count = 0;
   foreach $name (@cookies) {
      $text .= '   '.$name.' = '.$query->cookie($name)."\n";

#- Creating new cookies
   $cookie1 = CGI::Cookie->new(-name=>'Chocolate_'.$count,
      -value=>'Another chocolate cookie for you!');
   $cookie2 = CGI::Cookie->new(-name=>'Sugar_'.$count,
      -value=>'Another sugar cookie for you!');

#- Setting new cookies to the outgoing response
   print $query->header(-cookie=>[$cookie1, $cookie2]);
   print $query->start_html(-title=>'');
   print $query->pre($text);
   print $query->end_html();

Copy to C:\local\apache\htdocs and browse to: http://localhost/

The script will return 2 new cookies in the response header. But it will not return any cookie information on the page, because the browser didn't send any cookies to the server in the first request.

0 Cookies received from request:

If you refresh the browser, you should see 2 cookies on the page, because the browser sent 2 cookies received in the first response in the second request.

2 Cookies received from request:
   Sugar_2 = Another sugar cookie for you!
   Chocolate_1 = Another chocolate cookie for you!

Of course, if you refresh the browser again, you see more cookies showing up on the page, because the script is keep adding 2 new cookies each time it processes a request.

Table of Contents

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

 Data Types: Values and Variables

 Expressions, Operations and Simple Statements

 User Defined Subroutines

 Perl Built-in Debugger

 Name Spaces and Perl Module Files

 Symbolic (or Soft) References

 Hard References - Addresses of Memory Objects

 Objects (or References) and Classes (or Packages)

 Typeglob and Importing Identifiers from Other Packages

 String Built-in Functions and Performance

 File Handles and Data Input/Output

 Open Files in Binary Mode

 Open Directories and Read File Names

 File System Functions and Operations

 Image and Picture Processing

 Using DBM Database Files

 Using MySQL Database Server

 Socket Communication Over the Internet

 XML::Simple Module - XML Parser and Generator

 XML Communication Model

 SOAP::Lite - SOAP Server-Client Communication Module

 Perl Programs as IIS Server CGI Scripts

 CGI (Common Gateway Interface)

 XML-RPC - Remote Procedure Call with XML and HTTP

 RPC::XML - Perl Implementation of XML-RPC

 Integrating Perl with Apache Web Server Module for Building Web Pages

 What Is

 Generating HTML Document with

 Retrieving Query Parameters and Headers

 Retrieving Environment and Script Information

 Redirecting Browser to a URI

 "Refresh" http-equiv Meta Tag

 What Is CGI::Cookie?

Retrieving and Setting HTTP Cookies

 LWP::UserAgent and Web Site Testing

 Converting Perl Script to Executable Binary

 Managing Perl Engine and Modules on macOS

 Archived Tutorials


 Full Version in PDF/EPUB