Form and CGI Optimization

Forms can be used for anything from feedback and e-commerce applications to web-based services. However, they are seldom optimized for speed.

There is one required attribute to the form element: action . The action attribute specifies the URI of the application that will receive and process the form's data.

 <form action="/cgi-bin/search.cgi">...</form> 

The method attribute sets the HTTP method the browser uses to send the form's data to the server. There are two methods of sending data: POST or GET . With the POST method, the browser sends the data in two stepsfirst it contacts the server's action script; then it sends the data to the server in a separate transmission. The data is hidden from the user using this method.

In the GET method (the default) the browser contacts the server's action script and appends the data onto the URL, all in one transmission. GET s are more efficient than POST s, but less secure because the data appears and can be modified in the browser's address field.

Hidden controls often are used to pass default values to the server's CGI script using either method. However, these can quickly add up. Here's an example:

 <form method=get action="/cgi-bin/search.cgi">  <td> <span class="c"><a href="/cgi-bin/search.cgi">Search&#160;&gt;</a></span>&#160; </td> <td width="5%">       <input type="hidden" name="what" value="local">       <input type="hidden" name="engine" value="au"> <input type="hidden" name="summary" value="1"> <input type="hidden" name="startnumber" value="0"> <input type="hidden" name="batchsize" value="25"> <input type="hidden" name="relevancethreshold" value="50"> <input type="text"  name="query" size="12"> </td> <td width="1%"> <input type="hidden" name="Submit" value="Search"> <!-- for returns --> <input type="submit" name="Submit" value="Search"> </td> </form> 

A better way to set defaults is to shunt them to your CGI script. Here's an example based on WebReference .com's search.cgi Perl script (http://www.webreference.com/scripts/):

 # search.cgi - default entries for parameters  my $What_DEF         = 'local'; # search where?, locally, usenet, the web? my $Engine_DEF       = 'au';    # which search engine to use my $QuerySummary_DEF = '1';     # display summaries? 1=yes 0=no my $Startnumber_DEF  = '0';     # where to start output my $Batchsize_DEF    =  25;     # number of results to return my $Relevancethreshold= 50;     # relevance threshold, 0-100 my $Sort_DEF         = '';      # type of autonomy sort #                                   empty means Relevance, Date # form variables - from CGI my $webinfo     = new CGI;    $webinfo->autoEscape(undef);    # turn off automatic escaping of values my $What        = $webinfo->param('what');    # search Usenet, web, locally? my $Engine      = $webinfo->param('engine');  # which search engine to use my $Query_clean = $webinfo->param('query');   # the query w/o special chars my $Query       = uri_escape($Query_clean,"^A-Za-z"); # the query with special chars my $QuerySummary= $webinfo->param('querysummary');    # display summaries? my $Startnumber = $webinfo->param('startnumber');     # where to start output my $Batchsize   = $webinfo->param('batchsize'); # number of results to return my $Relevancethreshold=$webinfo->param('relevancethreshold'); # rel thrshld0-100 my $Sort        = $webinfo->param('sort');      # type of autonomy srch 2 perf # replace with defaults any unsupplied params $What           = $What_DEF   unless ($What); $Engine         = $Engine_DEF unless ($Engine); $QuerySummary   = $QuerySummary_DEF  unless ($QuerySummary eq "0"); $Startnumber    = $Startnumber_DEF   unless ($Startnumber); $Batchsize      = $Batchsize_DEF     nless ($Batchsize); $Relevancethreshold=$Relevancethreshold_DEF unless ($Relevancethreshold); $Sort           = $Sort_DEF     unless ($Sort); 

Note you can still specify the hidden defaults in your forms, but this code allows you to omit the defaults. Now you can remove the hidden fields and shrink the form dramatically:

 <form method="get" action="/cgi-bin/search.cgi">  <td> <span class="c"><a href="/cgi-bin/search.cgi">Search&#160;&gt;</a></span>&#160; </td> <td width="5%"> <input type="text" name="query" size="12"> </td> <td width="1%"> <input type="hidden" name="Submit" value="Search"> <!-- for returns --> <input type="submit" name="Submit" value="Search"> </td> </form> 

But don't stop there. By abbreviating the action URI and simplifying your layout, you can shrink it even more:

 <form class="tb" method="get" action="/r/cs">  <table border="0" cellspacing="0" cellpadding="0"> <tr><td><input type="text" name="query" size="20"></td><td width="1%" graphics/ccc.gif align="right"><input type="submit" value="search"></td> </tr></table></form> 

Note that we've expanded the search field from 12 to 20 characters for better usability here. You could go even further, because get is the default method , text is the default input type, and submit is the default value for submit -type inputs:

 <form class="tb" action="/r/cs">  <table border="0" cellspacing="0" cellpadding="0"> <tr><td><input name="query" size="20"></td><td width="1%"><input graphics/ccc.gif type="submit"></td> </tr></table></form> 

This method saves 485 bytes or 70 percent from the original (685 to 200 bytes). The form will say "Submit" instead of "Search" now of course, which isn't too specific. But for some forms, this would be appropriate. Strip out the presentation, and the original 18-line form fits into one line:

 <form action="/r/cs"><input name="q" size="20"><input type="submit" value="search"></ graphics/ccc.gif form> 

Parameter-free forms like this one save space and are required by some third-party web services, like Apple's Sherlock search service for the Macintosh OS.

Use mod_perl to Speed Up Perl Scripts

Another useful module for speeding up Apache-based sites is mod_perl. The mod_perl module embeds a Perl interpreter within your web server, avoiding the overhead of starting the Perl interpreter for each CGI request. Popular sites like Slashdot.org, Wired.com, and IMDB.com use mod_perl to speed their content delivery by orders of magnitude. mod_perl avoids the need to fork a new process for each script request, which can overload your server's RAM and process limits.

Existing scripts can be used with the Apache::PerlRun module and with a little tweaking moved to Apache::Registry for better performance. mod_perl is second only to PHP for module popularity, with over 36 percent share, according to SecuritySpace.com. For more information, see http://perl.apache.org/.

 



Speed Up Your Site[c] Web Site Optimization
Speed Up Your Site[c] Web Site Optimization
ISBN: 596515081
EAN: N/A
Year: 2005
Pages: 135

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