• Aucun résultat trouvé

Typical Uses of CGI Scripts

Dans le document From the authors (Page 158-164)

CGI programs and scripts allow you to have a site that provides func-tionality that’s similar to a desktop application. By itself, HTML can only be used to create Web pages that display the information that is specified when the Web page is created. It will show the text that was typed in when the page was created, and various graphics that you specified. CGI allows you to go beyond this, and takes your site from providing static information to being dynamic and interactive.

CGI can be used in a number of ways. An example of CGI, shown in Figure 4.2, is its use by eBay, the online auction house. It uses CGI to

process bids, and process user logons to display a personal Web page of purchases and items being watched during the bidding process.This is similar to other sites that use CGI programs to provide shopping carts, CGI programs that keep track of items a user has selected to buy. Once the users decide to stop shopping, these customers use another CGI script to “check out” and purchase the items.

While sites such as eBay and e-commerce sites may use more com-plex CGI scripts and programs for making transactions, there are also a number of other common uses for CGI on the Web, including counters, which show the number of users who have visited a particular site. Each time a Web page is accessed, a CGI script is run that increments the counter number by one.This allows Webmasters to view how often a particular page is viewed, and the type of content that is being accessed most often.

Guest books and chatrooms are other common uses for CGI pro-grams. Chatrooms allow users to post messages, and chat with one another online.This allows users to exchange information, without having to exchange personal information.This provides autonomy to the Figure 4.2eBay’s Use of CGI for Its Online Auctions

users, while allowing them to discuss topics in a public forum. Guest books allow users to post their comments about the site to a Web page.

Users enter their comments and personal information (such as their name and/or e-mail address). Upon clicking Submit, the information is appended to a Web page, and can be viewed by anyone who wishes to view the contents of the guest book.

Another popular use for CGI is comment or feedback forms, which allow users to send e-mail to voice their concerns, praise, or criticisms about your site or your company’s product. In many cases, companies will use these for customer service, so that customers have an easy way to contact a company representative. Figure 4.3 shows a form that is used to solicit feedback from visitors. Users enter their name, e-mail address, and comments on this page.When they click Send, the infor-mation is sent to a specific e-mail address.

In looking at the HTML content of this page, we can see that there is very little involved in terms of the Web page itself. In the following code, a form has been created on this page.The POST method is used Figure 4.3Comment Form That Uses CGI to Send Feedback to an E-Mail Address

to pass information that’s entered into the various fields to a CGI pro-gram called comment.pl.The field information is placed into variables called name (for the person’s name),e-mail(for the e-mail address they entered), and feedback (for their personal comments). After the program processes the data it receives, an e-mail message will be sent to the address mcross@freebsd.org. All of this is specified through the various values attributed to the form fields.

<HTML>

<HEAD>

<TITLE>Send Comments</TITLE>

</HEAD>

<BODY BGCOLOR="#FFFFFF">

<H2>Comment Form</H2>

<FORM METHOD="post" ACTION="/cgi-bin/comment.pl">

<B>Name:&nbsp; </B><INPUT NAME="name" SIZE=50 TYPE="text"> <BR>

<B>E-mail:&nbsp; </B><INPUT NAME="e-mail" SIZE=50 TYPE="text"> <BR>

<INPUT TYPE="hidden" NAME="submitaddress"

VALUE="mcross@freebsd.org">

<P>&nbsp;<B>Comments:</B></P>

<P>

<TEXTAREA NAME="feedback" ROWS=10 COLS=50></TEXTAREA><P>

<CENTER>

<INPUT TYPE=submit VALUE="SEND">

<INPUT TYPE=reset VALUE="CLEAR">

</CENTER>

</FORM>

</BODY>

</HTML>

While the HTML takes the data, and serves as an instrument to use CGI to pass the variables, the script itself does the real work. In this case,

the script is written in Perl. In the code, comments begin with the pound symbol (“#”) and are ignored during processing.The code in the Perl script called comment.pl is as follows:

# The following specifies the path to the PERL interpreter.

# It must show the correct path, or the script will not work

#!/usr/local/bin/perl

# The following is used to accept the form data, which is used

# in processing

if ($ENV{'REQUEST_METHOD'} eq 'POST') {

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

@pairs = split(/&/, $buffer);

foreach $pair (@pairs) {

($name, $value) = split(/=/, $pair);

$value =~ tr/+/ /;

$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

$FORM{$name} = $value;

}

# The following code is used to send e-mail to the

# specified e-mail address

open (MESSAGE,"| /usr/lib/sendmail -t");

print MESSAGE "To: $FORM{submitaddress}\n";

print MESSAGE "From: $FORM{name}\n";

print MESSAGE "Reply-To: $FORM{email}\n";

print MESSAGE "Subject: Feedback from $FORM{name} at

$ENV{'REMOTE_HOST'}\n\n";

print MESSAGE "The user commented:\n\n";

print MESSAGE "$FORM{feedback}\n";

close (MESSAGE);

&thank_you;

}

# The following code creates a Web page that confirms

# e-mail was sent

sub thank_you {

print "Content-type: text/html\n\n";

print "<HTML>\n";

print "<HEAD>\n";

print "<TITLE>Thank You!</TITLE>\n";

print "</HEAD>\n";

print "<BODY BGCOLOR=#FFFFCC TEXT=#000000>\n";

print "<H1>Thank You!</H1>\n";

print "\n";

print "<P>\n";

print "<H3>Your feedback has been sent.<BR>\n";

print "<P>\n";

print "</BODY>\n";

print "</HTML>\n";

exit(0);

}

The beginning of the code specifies the location of the Perl inter-preter. In the case of the Web server on which this script was run, the Perl interpreter resides in the directory /usr/local/bin/perl.This is required by the program, because the interpreter is used to compile the script at the time it is executed (that is, when the user clicks Send).

Without this line of code, the script won’t be able to compile, and will be unable to run.

The next section of the program is used to accept the data from the form on the Web page.This is so that the data can be processed, and used in the next section, where the data in each variable is put into an e-mail message. Once this is done, the final section of script is executed.

Here, a Web page is produced and returned to the user who initially entered the data.This HTML document confirms that the feedback was sent, so that the user knows the task is done and he or she can continue browsing your site.

Dans le document From the authors (Page 158-164)