Chapter: Internet & World Wide Web HOW TO PROGRAM - Perl and CGI (Common Gateway Interface)

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

Server-Side Includes - Perl

Dynamic content greatly improves the look and feel of a Web page. Pages that include the current date or time, rotating banners or advertisements, a daily message, special offer or company news are attractive, because they are current.

Server-Side Includes

 

Dynamic content greatly improves the look and feel of a Web page. Pages that include the current date or time, rotating banners or advertisements, a daily message, special offer or company news are attractive, because they are current. Clients see new information on ev-ery visit and thus will likely revisit the site in the future.

 

Server-Side Includes (SSIs) are commands embedded in XHTML documents to allow the creation of simple dynamic content. SSI commands like ECHO and INCLUDE enable the inclusion on Web pages of content that is constantly changing (i.e., the current time) or information that is stored in a database. The command EXEC can be used to run CGI scripts and embed their output directly into a Web page.

 

Not all Web servers support the available SSI commands. Therefore, SSI commands are written as XHTML comments (e.g., <!--#ECHO VAR="DOCUMENT_NAME" -->). Servers that do not recognize these commands treat them as comments. Some servers do support SSI commands, but only if the servers are configured to do so. Check your server’s documentation to configure your server appropriately.

 

A document containing SSI commands is typically given the .shtml file extension (the s at the front of the extension stands for server). The .shtml files are parsed by the server. The server executes the SSI commands and writes output to the client.

 

Figure 27.14 implements a Web page hit counter. Each time a client requests the doc-ument, the counter is incremented by 1. The Perl script fig27_15.pl manipulates the counter.

 

 

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

 

    <!-- Fig. 27.14: fig27_14.shtml -->

 

 

      <html>

      <head>

      <title>Using Server Side Includes</title>

      </head>

        <body>

        <h3 style = "text-align: center">

        Using Server Side Includes

        </h3>

              <!--#EXEC CGI="/cgi-bin/fig27_15.pl" --><br />

 

        The Greenwich Mean Time is

        <span style = "color: blue">

                  <!--#ECHO VAR="DATE_GMT" -->.

 

              </span><br />

 

        The name of this document is

        <span style = "color: blue">

        <!--#ECHO VAR="DOCUMENT_NAME" -->.

        </span><br />

        The local date is

        <span style = "color: blue">

        <!--#ECHO VAR="DATE_LOCAL" -->.

        </span><br />

        This document was last modified on

        <span style = "color: blue">

        <!--#ECHO VAR="LAST_MODIFIED" -->.

        </span><br />

        Your current IP Address is

        <span style = "color: blue">

        <!--#ECHO VAR="REMOTE_ADDR" -->.

        </span><br />

        My server name is

        <span style = "color: blue">

        <!--#ECHO VAR="SERVER_NAME" -->.

        </span><br />

        And I am using the

        <span style = "color: blue">

                  <!--#ECHO VAR="SERVER_SOFTWARE" -->

        Web Server.

        </span><br />

 

        You are using

        <span style = "color: blue">

        <!--#ECHO VAR="HTTP_USER_AGENT" -->.

        </span><br />

        This server is using

        <span style = "color: blue">

        <!--#ECHO VAR="GATEWAY_INTERFACE" -->.

        </span><br />

        <br /><br />

<div style = "text-align: center;

    font-size: xx-small">

    <hr />

    This document was last modified on

        <!--#ECHO VAR="LAST_MODIFIED" -->.

        </div>

        </body>

</html>


 

Fig. 27.14 Incorporating a Web-page hit counter and displaying environment variables, using server-side includes

 

 

Line 16 of the fig27_14.shtml script executes the fig27_15.pl script, using the EXEC command. Before the XHTML document is sent to the client, the SSI command is executed, and any script output is sent to the client. This technique can increase the load on the server tremendously, depending on how many times the script has to be parsed and the size and workload of the scripts.

 

Line 20 uses the ECHO command to display variable information. The ECHO command is followed by the keyword VAR and the name of the variable. For example, variable DATE_GMT contains the current date and time in Greenwich Mean Time (GMT). In line 25, the name of the current document is included in the XHTML page with the DOCUMENT_NAME variable. The DATE_LOCAL variable inserts the date on line 30 in local format—different formats are used around the world.

 

Figure 27.15 (fig27_15.pl) introduces file input and output in Perl. Line 8 opens (for input) the file counter.dat, which contains the number of hits to date for the fig27_14.shtml Web page. Function open is called to create a filehandle to refer to the file during the execution of the script. In this example, the file opened is assigned a file-handle named COUNTREAD.

 

    #!/usr/bin/perl

    # Fig. 27.15: fig27_15.pl

# Program to track the number of times

    # a Web page has been accessed.

 

    use CGI qw( :standard );

 

    open( COUNTREAD, "counter.dat" );

    $data = <COUNTREAD>;

    $data++;

    close( COUNTREAD );

 

    open( COUNTWRITE, ">counter.dat" );

    print( COUNTWRITE $data );

    close( COUNTWRITE );

   

    print( header(), "<div style = \"text-align: center;

    font-weight: bold\">" );

    print( "You are visitor number", br() );

   

    for ( $count = 0; $count < length( $data ); $count++ ) {

    $number = substr( $data, $count, 1 );

    print( img( { src => "images/$number.gif" } ), "\n" );

    }

 

    print( "</div>" );


Fig. 27.15  Perl script for counting Web page hits

 

Line 9 uses the diamond operator, <>, to read one line of the file referred to by file-handle COUNTREAD and assign it to the variable $data. When the diamond operator is used in a scalar context, only one line is read. If assigned to an array, each line from the file is assigned to a successive element of the array. Because the file counter.dat contains only one line (in this case, only one number), the variable $data is assigned the value of that number in line 9. Line 10 then increments $data by 1. If the file does not yet exist when we try to open it, $data is assigned the value undef, which will be evaluated as 0 and incremented to 1 on line 10.

 

Now that the counter has been incremented for this hit, we write the counter back to the counter.dat file. In line 13 we open the counter.dat file for writing by pre-ceding the file name with a > character (this is called write mode). This immediately trun-cates (i.e., discards) any data in that file. If the file does not exist, Perl creates a new file with the specified name. Perl also provides an append mode (>>) for appending to the end of a file. The first argument (COUNTWRITE) specifies the filehandle, which will be used to refer to the file.

 

After line 13 is executed, data can be written to the file counter.dat. Line 14 writes the counter number back to the file counter.dat. The first argument to print indi-cates the filehandle that refers to the file where data are written. If no filehandle is specified, print writes to standard out (STDOUT). Notice that we need to use a space, rather than a comma, to separate the filehandle from the data. In line 15, the connection to counter.dat is terminated by calling function close.

 

Lines 21–24 use a for structure to iterate through each digit of the number scalar $data. The for structure syntax consists of three semicolon-separated statements in parentheses, followed by a body delimited by curly braces. In our example, we iterate until $count is equal to length( $data ). Function length returns the length of a char-acter string, so the for iterates once for each digit in the variable $data. For instance, if $data stores the value "32", then the for structure iterates twice, first to process the value "3", and second to process the value "2". In the first iteration, $count equals 0 (as initialized on line 21), and the second time $count will equal 1. This is because the value of $count will be incremented for each loop (as specified by the statement $count++, also on line 21). For each iteration, we obtain the current digit by calling function substr. The first parameter passed to function substr specifies the string from which to obtain a substring. The second parameter specifies the offset, in characters, from the beginning of the string, so an offset of 0 returns the first character, 1 returns the second and so forth. The third argument specifies the length of the substring to be obtained (one character in this case). The for structure then assigns each digit (possibly from a multiple-digit number) to the scalar variable $number. Each digit’s corresponding image is displayed using the img function (line 23).

 

It is important in this example to think about file permissions and security. This program may not run correctly if the user’s default settings do not allow scripts to manipulate files. In order to resolve this issue, the user can change the permissions in the folder where counter.dat resides, so that all users have Write access. However, the user should be aware that this poses a security risk to the system. Security details are covered in Chapter 32.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.