📄 ch13.htm
字号:
a cookie. The home page is then altered to include links to pages
that the user visits frequently. Some users feel that having their
movements tracked in that way is a violation of their privacy-despite
the fact that all the information gathered from the cookies is
also available, albeit harder to interpret, from the logs of the
Web server. It's generally a safe plan, if you really want to
track a user's preferences, to inform the user of this up front
and allow him the option of not having his information logged.
<P>
For an example of HTTP cookies, turn to their other major application:
the "shopping cart." A Web shopping cart is simply a
method for allowing the end user to browse a number of pages,
selecting any number of items from HTML forms on those pages and
storing the selections for later use. There need not be any actual
shopping involved. In fact, the next example would be impractical
for real commerce because no security is involved.
<P>
<CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Tip</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
In this example, a module called <TT><FONT FACE="Courier">cgi_head.pm</FONT></TT> is used to gather the information from the form. This module places a form entry with the name <TT><FONT FACE="Courier">'foo'</FONT></TT> into an associative array entry with
name <TT><FONT FACE="Courier">$FORM{'foo'}</FONT></TT>. Included in this module is the subroutine you saw previously, which loads all cookie information into an associative array called <TT><FONT FACE="Courier">%COOKIE</FONT></TT>. There are several freely
available programs for several languages to accomplish this, including <TT><FONT FACE="Courier">CGI.pm</FONT></TT> for Perl at <TT><FONT FACE="Courier">http://www.perl.com/perl/CPAN/</FONT></TT>.
</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<P>
For simplicity's sake, the shopping cart is contained in one CGI
program that produces one HTML page. The real power of shopping
carts is that they can span any number of pages, remembering the
user's items throughout. Listing 13.3 displays the simple shopping
cart.
<HR>
<BLOCKQUOTE>
<B>Listing 13.3. Simple shopping cart.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT>#!/usr/local/bin/perl -Tw<BR>
<BR>
require cgi_head; # Get %FORM and %COOKIE hashes.<BR>
<BR>
# Now we create an associative array that contains information
about our<BR>
# product. This information could be gathered in any method: from
a flat file,<BR>
# from a database, from another process, etc. This information
isn't even<BR>
# necessary for the actual program to run, it just provides a
more realistic<BR>
# simulation.<BR>
%whatzit = (<BR>
Blue => '14.95',<BR>
Red => '12.50',<BR>
Big => '19.95',<BR>
Tiny => '4.95',<BR>
Paisely => '99.95'<BR>
);<BR>
<BR>
# We set a variable with the date as it will be 3 hours from now
(10800<BR>
# seconds is 3 hours).<BR>
$later = time + 10800;<BR>
# We now convert the time into the format required by the Cookie
syntax.<BR>
$date = gmtime($later);<BR>
<BR>
# The browser didn't send us any cookies, this must be its first
trip<BR>
# here. All we need to do is assign it an ID number and print
out the<BR>
# introductory HTML form.<BR>
if (!%COOKIE) {<BR>
<BR>
# The user's ID number is a combination
of the current time and<BR>
# the current process id number. Since
any two processes running<BR>
# at the same time will have different
id numbers, this creates a<BR>
# pretty much unique number.<BR>
$id = time . $$;<BR>
<BR>
###########################IMPORTANT###################################
<BR>
# The second line below is the actual 'Set-cookie' HTTP header. #
<BR>
# The name of the cookie is the ID number, and the value is '0'. #
<BR>
# (The ID number is preceded by 'mycartid' just in case someone #
<BR>
# else sends the user a cookie with the same ID number. Of course #
<BR>
# someone could send them a cookie that begins with 'mycartid'
as #<BR>
# well, but the more complex your cookie name is, the less likely
#<BR>
# it is to be accidentally duplicated. The value will
be replaced #<BR>
# by whatever items the user chooses. The ''expires'' field is
set to #<BR>
# the $date variable we create above which is 3 hours from now. #
<BR>
# This can be changed to whatever time is needed. The path should
#<BR>
# be changed from '/~me/' to whatever URL path points to the #
<BR>
# directory where this CGI is located. The domain should be changed
#<BR>
# from 'my.isp.com' to whatever your machine name is.
#<BR>
#######################################################################
<BR>
<BR>
print <<EOP;<BR>
Content-type: text/html<BR>
Set-cookie: mycartid$id=0; expires=$date; path=/~me/; domain=my.isp.com
<BR>
<BR>
<HTML><HEAD><TITLE>Welcome to the Whatzit Emporium!</title></head><BODY>
<BR>
<H1 ALIGN=CENTER>The Whatzit Emporium</h1><BR>
<p>Greetings and welcome to the Whatzit Emporium. Here you
will find<BR>
Whatzitses of all shapes, sizes and colors to suit your Whatziting
needs.<BR>
<BR>
<p>To add a Whatzit to your cart, simply click on the 'Pick
Me!' button<BR>
next to it. When you are ready to leave, simply click on the 'Check
Out!'<BR>
button on the bottom of the screen.<BR>
<BR>
<p><BR>
<FORM ACTION="cart.cgi" METHOD=POST><BR>
<UL><BR>
<LI><INPUT TYPE="SUBMIT" NAME="Blue"
VALUE=" Pick Me! "> Blue Whatzit<BR>
\$$whatzit{'Blue'}<BR>
<LI><INPUT TYPE="SUBMIT" NAME="Red"
VALUE=" Pick Me! "> Red Whatzit<BR>
\$$whatzit{'Red'}<BR>
<LI><INPUT TYPE="SUBMIT" NAME="Big"
VALUE=" Pick Me! "> Big Whatzit<BR>
\$$whatzit{'Big'}<BR>
<LI><INPUT TYPE="SUBMIT" NAME="Tiny"
VALUE=" Pick Me! "> Tiny Whatzit<BR>
\$$whatzit{'Tiny'}<BR>
<LI><INPUT TYPE="SUBMIT" NAME="Paisley"
VALUE=" Pick Me! "> Paisley<BR>
Whatzit<BR>
\$$whatzit{'Paisley'}<BR>
</ul><BR>
<hr><BR>
<INPUT TYPE=SUBMIT NAME="Checkout" VALUE=" Check
Out! "><BR>
</form><BR>
</body></html><BR>
EOP<BR>
die;<BR>
}<BR>
# Notice that in the form above we don't have lots of input fields
and one<BR>
# submit button, but rather lots of submit buttons and no fields.
This is<BR>
# a handy technique for when you have a set of several things
for the user<BR>
# to choose from but no other information is needed.<BR>
<BR>
<BR>
# If there are cookies to look at, look at each one.<BR>
CLOOP:<BR>
foreach (%COOKIE) {<BR>
<BR>
# If the cookie begins with 'mycartid'
we assume it is one of<BR>
# ours.<BR>
if ($_ =~ /^mycartid(.*)$/) {<BR>
# Grab the $id
number from the name.<BR>
$id = $1;<BR>
# Grab the cookie
value.<BR>
$cookie = $COOKIE{$_};
<BR>
# Since we only
send one cookie to each browser, now that we<BR>
# have it, we
don't need to look at any other cookies.<BR>
last CLOOP;<BR>
}<BR>
}<BR>
# Dump the unneeded cookies.<BR>
undef %COOKIE;<BR>
<BR>
# If the cookie value is '0' they just came from the intro page
and have<BR>
# nothing in their cart.<BR>
if ($cookie = '0') { $cookie = "");<BR>
<BR>
# When we set the cookie value, we separate each type of Whatzit
with a<BR>
# '+'. Now we split them into an array.<BR>
@items = split(/+/,$cookie);<BR>
<BR>
foreach (@items) {<BR>
# When we set the cookie, we separate
the type of Whatzit from the<BR>
# amount requested with '-'. Now we split
each one into a type and<BR>
# an amount.<BR>
( $type, $amount ) = split(/-/,$_);<BR>
# We set the variable ${$type} to the
amount of the that type. So<BR>
# the variable $Blue will hold the number
of Blue Whatzitses.<BR>
${$type} = $amount;<BR>
}<BR>
<BR>
foreach (keys %whatzit) {<BR>
# For each type of Whatzit (defined at
the top of the script) we<BR>
# check to see if the user just added
onto their cart. If so,<BR>
# we increment
the corresponding variable.<BR>
if ($FORM{$_}) { ${$_}++; }<BR>
# If, after adding the most recent entry,
there are any of this<BR>
# type, add it and the amount held to
an array.<BR>
if (${$_}) { push(@cstring, "$_-${$_}");
}<BR>
}<BR>
# Combine the types and amounts held into one big string.<BR>
# This string would look something like this:<BR>
# Blue-3+Red-1+Tiny-1<BR>
$cstring = join('+', @cstring);<BR>
<BR>
if ($FORM{'Checkout'}) {<BR>
<BR>
# If the user wants to checkout, we first
clear their cookie, then<BR>
# print the start of the checkout message.
<BR>
print <<EOP;<BR>
Content-type: text/html<BR>
Set-cookie: $id=; expires=$date; path=/~me/; domain=my.isp.com
<BR>
<BR>
<HTML><HEAD><TITLE>The Whatzit Emporium!</title></head><BODY>
<BR>
<H1 ALIGN=CENTER>The Whatzit Emporium</h1><BR>
<p>Thank you for shopping the Whatzit Emporium! We hope
you have enjoyed<BR>
your visit. Here are the totals of your shopping trip:<BR>
<BR>
<p><BR>
EOP<BR>
<BR>
# Now for each of the Whatzit types, we
multiply the number of<BR>
# Whatzit's in the cart by the price of
each Whatzit.<BR>
foreach (keys %whatzit) {<BR>
if (${$_}) {<BR>
my
$subtotal = ${$_} * $whatzit{$_};<BR>
print
<BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -