10.07.2015 Views

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 1 ■ THE CGI MODULE 23You set the secure flag as another argument within the call <strong>to</strong> the cookie function, as inthis example:cookie(-name=>'cookiename',-value=>'cookievalue',-secure=>1);Note that if you don’t have an SSL-enabled web server, you won’t be able <strong>to</strong> test the secureflag.Environment VariablesEnvironment variables are values set by the environment where a process runs. In the case ofa CGI script, the environment variables are set by the web server on which they run. Thesevariables are au<strong>to</strong>matically made available <strong>to</strong> your CGI script in the form of the %ENV hash.Viewing Environment VariablesThe script shown in Listing 1-12 makes it possible <strong>to</strong> view the environment variables available<strong>to</strong> a CGI script on your web server.Listing 1-12. Viewing Environment Variables in a CGI Script#!/usr/bin/perl -Tuse strict;use CGI qw/:standard/;print header,start_html('Environment Variables');foreach my $variable (keys %ENV) {print p("$variable is $ENV{$variable}");}print end_html;exit;The only new items in this script are the three lines of the foreach loop, which is a standard<strong>Perl</strong> construct. Saving this script <strong>to</strong> your web server and accessing it through a browserwill result in a page similar <strong>to</strong> that in Figure 1-6.Some of the environment variables sent <strong>to</strong> your script by the web server are the result ofsettings on the server itself; others are sent from the server as read by the browser accessingthe script. One environment variable is HTTP_USER_AGENT. This environment variable is sometimesused (and misused) <strong>to</strong> determine the browser software and version accessing the page.Chapter 2 examines the use of HTTP_USER_AGENT <strong>to</strong> present a page based on the value of thevariable.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!