[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

last two sections, generating query parameters on URL links and hidden form fields in
input pages sent as replies are two simple ways for a CGI script to pass data to the next
script in the application. When clicked or submitted, such parameters send
preprogrammed selection or session information back to another server-side handler
script. In a sense, the content of the generated reply page itself becomes the memory
space of the application.


For example, a site that lets you read your email may present you with a list of viewable
email messages, implemented in HTML as a list of hyperlinks generated by another
script. Each hyperlink might include the name of the message viewer script, along with
parameters identifying the selected message number, email server name, and so on—
as much data as is needed to fetch the message associated with a particular link. A retail
site may instead serve up a generated list of product links, each of which triggers a
hardcoded hyperlink containing the product number, its price, and so on. Alternatively,
the purchase page at a retail site may embed the product selected in a prior page as
hidden form fields.


In fact, one of the main reasons for showing the techniques in the last two sections is
that we’re going to use them extensively in the larger case study in the next chapter.
For instance, we’ll use generated stateful URLs with query parameters to implement
lists of dynamically generated selections that “know” what to do when clicked. Hidden
form fields will also be deployed to pass user login data to the next page’s script. From
a more general perspective, both techniques are ways to retain state information be-
tween pages—they can be used to direct the action of the next script to be run.


Generating URL parameters and hidden form fields works well for retaining state in-
formation across pages during a single session of interaction. Some scenarios require
more, though. For instance, what if we want to remember a user’s login name from
session to session? Or what if we need to keep track of pages at our site visited by a
user in the past? Because such information must be longer lived than the pages of a
single session of interaction, query parameters and hidden form fields won’t suffice. In
some cases, the required state information might also be too large to embed in a reply
page’s HTML.


In general, there are a variety of ways to pass or retain state information between CGI
script executions and across sessions of interaction:


URL query parameters
Session state embedded in generated reply pages


Hidden form fields
Session state embedded in generated reply pages


Cookies
Smaller information stored on the client that may span sessions


Server-side databases
Larger information that might span sessions


Saving State Information in CGI Scripts| 1175
Free download pdf