Setting Up a Virtual Storefront

only for RuBoard - do not distribute or recompile

The rest of this chapter is devoted to setting up a small site that serves as the e-commerce base for a fictitious company, PseudEcom Corporation,[2] and that implements several of the functions listed in the chapter introduction. In this section, we ll design a storefront home page that provides general information about the site and points to its other major areas. Then we ll cover implementation of those areas in the sections to follow.

[2] PseudEcom = pseudo (fake) e-com, pronounced SOO-de-com.

A helpful and informative home page is important for making a good first impression on people who arrive at that page when they first visit your site. However, it s also important to realize that many people won t reach you that way. Your site undoubtedly will be indexed by search engines (whether or not you explicitly request it), and most of the time these engines will take people to pages other than your home page. For example, people looking for a particular product may find a reference in a search engine that jumps them directly to a page that displays an item from your product catalog.

Don t make the mistake of thinking that you have any control over how people come into your site. Instead, plan in advance to handle this issue by providing good navigation aids on every page. At a minimum, each page should have a link to your home page, but you can also provide links to other major site areas. A useful technique for assisting visitors is to put a standard navigation area on each page. This provides a consistent point of reference that helps them move about. It also allows users to gain a sense of familiarity with your site more quickly. That s a good thing because it helps your visitors feel at home. Other standard page layout features can be used, such as a company logo to help brand pages with a common look or identity, or a copyright notice to make your legal department happy. You can also include a for testing purposes only notice during development, and then remove it when the site goes live.

In a script-based environment, standard layout elements can be implemented different ways. For example, you could write a function that produces these elements and invoke it from all parts of your scripts that produce pages. Another approach (the one we ll use) is to generate the main content of each page in memory as a string. Then, when you reach the end of the script and you re ready to display the page to the client, you can pass the string to a function that adds the standard page elements (or boilerplate ). One advantage of using a script to produce boilerplate is that you re not limited to producing static standard elements. For example, you can include banner ads as part of your page layout and choose the advertisement to show at page display time.

For the PseudEcom site, the standard look divides pages into four sections the company name to the upper left, a navigation panel containing links to major site areas down the left side of the page, a small message area along the top, and the rest of the page devoted to the main content area. The PseudEcom home page shown in Figure 10.1 demonstrates this layout.

Figure 10.1. PseudEcom home page.
graphics/10fig01.gif

As you can see from the figure, the main areas supported by the PseudEcom site and listed on the home page are as follows:

  • Two shopping areas, one for financial assistance documents and one for the oh-so-essential pet-care items you need to keep those little loved ones happy. (If you were building your own site, you d probably integrate your product lines into a single interface. The PseudEcom site involves two separate applications to illustrate different approaches to online sales.)

  • The customer satisfaction guarantee.

  • A way for customers to submit comments or feedback or to ask questions.

  • Company contact information.

  • The company privacy policy.

  • A rate this site poll to solicit customer opinions.

Implementation of these various site functions varies quite a bit in complexity. Some of them (statements of policy, for example) are quite simple, the shopping applications are the most complex, and the others lie somewhere between.

Because the page boilerplate is generated on-the-fly, every page on the PseudEcom site is produced by a script, even the storefront home page that serves as the entryway to the site. This script, store.pl, shows visitors what s available and provides access to its services:

 #! /usr/bin/perl -w  # store.pl - Present general PseudEcom Corporation home page  use strict;  use lib qw(/usr/local/apache/lib/perl);  use CGI qw(:standard escape escapeHTML);  use WebDB;  use WebDB::PseudEcom;  my $page = h3 ("Welcome to the PseudEcom Corporation Web site.")          . p ("You'll find a variety of products and services here to\n"              . "meet all your online shopping needs.")          . ul (         li ("Looking for financial stability? Check out our selection of\n"              . get_doc_shop_link ("finance-related documents")              . "."),          li ("Have a look at our\n"              . get_pet_shop_link ("pet-care product line")              . "."),          li ("Read our customer statisfaction\n"              . get_satisfaction_link ("guarantee")              . "."),          li ("Have a comment or question?\n"              . get_feedback_link ("Let us know")              . "."),          li ("How to\n"              . get_contact_link ("contact us")              . "."),          li ("Concerned about the confidentiality of information you\n"              . "submit to us? Read our\n"              . get_privacy_link ("privacy policy")              . "."),          li ("Want to rate our site? Click\n"              . get_rating_link ("here")              . ".")          );  $page = add_boilerplate ($page);    # add standard page elements  print header (),          start_html (-title => "PseudEcom Corporation", -bgcolor => "white"),          $page,          end_html ();  exit (0); 

This script does little more than present a bit of introductory welcome text, followed by a list of the site s services. The items in this list provide links to the same parts of the site as the navigation panel that lies along the left edge of each PseudEcom page, but the associated text is more descriptive. (The get_xxx_link() routines used in creating the list are functions that generate hyperlinks to the major site areas. These functions are located in WebDB::PseudEcom, a module that provides support routines for PseudEcom scripts; it s described in the next section.) After generating the main page content and saving it in the $page variable, store.pl calls add_boilerplate() to embed the content within the standard page layout elements, and then displays the result.

Support Routines

The WebDB::PseudEcom support module provides access to functions that are used in common by the scripts that implement the PseudEcom site. The module source file PseudEcom.pm can be found in the WebDB directory of the book s webdb source distribution. Copy it to a WebDB directory that is under some directory in your Perl script search path. (This is similar to the way the WebDB::TableInfo and WebDB::Poll modules were installed in Chapter 6, Automating the Form-Handling Process. See that chapter for further information about module installation.)

The WebDB::PseudEcom module file begins with a package statement naming the module and some use statements referencing the other modules that it requires. That s much like the other modules we ve written up to this point. However, the module also does something we haven t done before it uses Exporter to export the names of some of its functions as public routines. This allows any of these functions to be invoked without naming the package. That is, a function f() can be called as just f() rather than as WebDB::PseudEcom::f().[3] The initial part of WebDB::PseudEcom looks like this:

[3] Should we have taken this same approach of exporting names from the modules that we wrote earlier in the book?

 package WebDB::PseudEcom;  # Arrange to export the "public" function names  require Exporter;  @ISA = qw(Exporter);  @EXPORT = qw(     get_url_prefix      get_home_link      get_doc_shop_link      get_pet_shop_link      get_contact_link      get_satisfaction_link      get_privacy_link      get_feedback_link      get_rating_link      get_card_type_list      get_card_exp_month_list      get_card_exp_year_list      add_boilerplate      fatal_error  );  use strict;  use lib qw(/usr/local/apache/lib/perl);  use CGI qw(:standard escape escapeHTML);  use WebDB; 

Most of the WebDB::PseudEcom module is devoted to uninteresting functions that generate URLs or hyperlinks to the major site areas. These encapsulate the details of the site layout into a functional interface to make it easier to rearrange the site if you like. (You might not want to use the same layout as the one that I show in this chapter.) However, these functions also are somewhat mindless to write, so I won t show them all. The following routine, get_home_link(), is a representative example. It returns a string that contains a hyperlink pointing to store.pl, the home page script:

 sub get_home_link  { my $label = shift;      return (get_link (get_url ("store.pl"), $label));  } 

You can see how get_home_link() is used by looking back at the source for store.pl, which also shows instances for most of the other link generators. To see how these functions are written (as well as other helper routines such as get_link() and get_url()), have a look at the PseudEcom.pm source file.

Another support routine is add_boilerplate(), the function that adds standard layout elements to a page. It uses a 2 2 table to divide the page into quadrants and places the main page content in the lower-right section:

 sub add_boilerplate  { my $page = shift;      return (table (Tr (             # upper left corner              td ("We are ..." .. br () . strong ("PseudEcom!")),              td (),                              # spacer column              # message at top of page              td ({-bgcolor => "silver"},                  "Note: this site currently is operating in test mode")          ),          Tr (td ({-colspan => "3"})),            # spacer row          Tr (             # navigation panel              td ({-bgcolor => "lightblue",                      -align => "center", -valign => "top"},                      get_home_link ("Home")                      . br () . br ()                      . get_doc_shop_link ("DocShop")                      . br () . br ()                      . get_pet_shop_link ("PetShop")                      . br () . br ()                      . get_satisfaction_link ("Customer guarantee")                      . br () . br ()                      . get_feedback_link ("Comments/questions")                      . br () . br ()                      . get_contact_link ("Contact us")                      . br () . br ()                      . get_privacy_link ("Privacy policy")                      . br () . br ()                      . get_rating_link ("Rate this site")                      ),              td (),                              # spacer column              # main page content              td ({-align => "left", -valign => "top"}, $page)          )));  } 

add_boilerplate() is used by all the PseudEcom scripts to produce pages. Therefore, if you want to modify the overall appearance of the site, this is the routine to tinker with. The boilerplate function is even used by fatal_error(), a WebDB::PseudEcom utility routine that places error message text within the standard boilerplate when terminal conditions occur:

 sub fatal_error  { my $msg = shift;  my $page;      $page .= p ("An error has occurred.")              . p (escapeHTML ($msg))              . p ("Return to the PseudEcom " .. get_home_link ("home page"));      print header (),              start_html (-title => "PseudEcom Corporation",                          -bgcolor => "white"),              add_boilerplate ($page),              end_html ();      exit (0);  } 

Other functions in this module file are discussed as the need arises. For example, the module includes routines that help generate fields for collecting credit card information. These are covered when we implement the first shopping application.

Configuring Apache to Recognize the Site

Where should the storefront home page script store.pl go? For this chapter, I ll just assume that you install store.pl in the cgi-perl directory and invoke it the usual way:

 http://www.snake.net/cgi-perl/store.pl 

On the other hand, if your entire server will be devoted to e-commerce functions, it s likely you d want the script to be invoked whenever a user enters into a browser nothing more than your domain name or the most likely guess for your home page:

 http://www.snake.net/  http://www.snake.net/index.html 

When Apache sees either URL, normally it will expect to serve a page named index. html from the top level of your document tree. If you want it to execute store. pl instead, you can trap these URLs and map them onto the storefront script using redirection directives in the Apache configuration file. For example, if store. pl is installed in the cgi-perl directory, the following two directives will accomplish this:

 RedirectMatch ^/$ http://www.snake.net/cgi-perl/store.pl  RedirectMatch ^/index.html$ http://www.snake.net/cgi-perl/store.pl 

If your site is not devoted entirely to e-commerce functions, you may want to segregate the PseudEcom store as a separate area of the site, for example, to be accessed through URLs that begin like this:

 http://www.snake.net/store/... 

In this case, you ll need to make different adjustments to your Apache configuration.

Whichever way you configure your site, at this point you should install the WebDB::PseudEcom module and the store.pl script, and then invoke the latter to try it out. It should present a page in your browser that resembles Figure 10.1, although none of the links except the Home link will work yet, because we haven t yet implemented any of the other areas of the site. That s covered in the following sections.

only for RuBoard - do not distribute or recompile


MySQL and Perl for the Web
MySQL and Perl for the Web
ISBN: 0735710546
EAN: 2147483647
Year: 2005
Pages: 77
Authors: Paul DuBois

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net