Deitel & Associates, Inc. Logo

Back to
digg.png delicious.png blinkit.png furl.png
Internet & World Wide Web How to Program, 3/e
Internet & World Wide Web How to Program, 3/e

© 2004
pages: 1420

Amazon logo

This tutorial shows how to use Perl server-side includes (SSIs) to add dynamic content to a Web page. In particular, we demonstrate a Web page hit counter. The tutorial is intended for students and professionals who are already familiar with Perl programming.
Download the Code Example
[Note: This tutorial is an excerpt (Section 25.6) of Chapter 25, Perl, from our textbook Internet & World Wide Web How to Program, 3/e. This tutorial may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., INTERNET & WORLD WIDE WEB HOW TO PROGRAM, 3/E, 2004, pp.866-870. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]
25.6 Server-Side Includes (Continued)

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 in line 30 in local format-different formats are used around the world.
Figure 25.4 ( 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 fig25_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 filehandle named COUNTREAD.
Fig. 25.4 Perl script for counting Web page hits.
1   #!C:\Perl\bin\perl
2   # Fig. 25.15:
3   # Program to track the number of times
4   # a Web page has been accessed.
6   use CGI qw( :standard );
8   open( COUNTREAD, "counter.dat" );
9   $data = <COUNTREAD>;
10  $data++;
11  close( COUNTREAD );
13  open( COUNTWRITE, ">counter.dat" );
14  print( COUNTWRITE $data );
15  close( COUNTWRITE );
17  print( header(), "<div style = \"text-align: center;
18                   font-weight: bold\">" );
19  print( "You are visitor number", br() );
21  for ( $count = 0; $count < length( $data ); $count++ ) {
22     $number = substr( $data, $count, 1 );
23     print( img( { src => "images/$number.gif" } ), "\n" );
24  }
26  print( "</div>" );

Page 1 | 2 | 3 | 4
Return to Tutorial Index