Perl Tutorials - Herong's Tutorial Examples - v6.01, by Dr. Herong Yang
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: CGI-pm-Manage-Cookies.pl, to test the CGI::Cookie class:
#!c:/local/perl/bin/perl.exe #- CGI-pm-Manage-Cookies.pl #- Copyright (c) HerongYang.com. 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) { $count++; $text .= ' '.$name.' = '.$query->cookie($name)."\n"; } #- Creating new cookies $count++; $cookie1 = CGI::Cookie->new(-name=>'Chocolate_'.$count, -value=>'Another chocolate cookie for you!'); $count++; $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=>'CGI-pm-Manage-Cookies.pl'); print $query->pre($text); print $query->end_html();
Copy CGI-pm-Manage-Cookies.pl to C:\local\apache\htdocs and browse to: http://localhost/CGI-pm-Manage-Cookies.pl
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
Data Types: Values and Variables
Expressions, Operations and Simple Statements
Name Spaces and Perl Module Files
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 Directories and Read File Names
File System Functions and Operations
Socket Communication Over the Internet
XML::Simple Module - XML Parser and Generator
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
►CGI.pm Module for Building Web Pages
Generating HTML Document with CGI.pm
Retrieving Query Parameters and Headers
Retrieving Environment and Script Information
►Retrieving and Setting HTTP Cookies
LWP::UserAgent and Web Site Testing
Converting Perl Script to Executable Binary