Counting the Number of Accesses





In order to keep track of accesses, we'll store the number of accesses in a text The text file will simply contain an integer representing the number accesses. Normally, you could divide this process into three steps:
Read the number from the file.
Increment the number.
Store the new number in the file.
Perlo Code
$data='/usr/local/etc/httpd/htdocs/counter.data';
# read the data
open(DATA,"$data")|| die "Can't open data file\n";
$accesses=;
chomp $(accesses);
$accesses++;
close(DATA);
#write new value
open(DATA, ">$data");
print DATA "$accesses\n";
close(DATA);
print "Content-type:text/text\n\n";
print $accesses;
You are ready to display the number.
Now the counter.html file
<html>
<head><title>Counter Using SSI</title>
</head>
<body>No of times this page has been accessed are
<!---#exec cgi=/cgi-bin/counter.cgi---->
</body>
</html>
where counter.cgi is the name of the CGI program. You need to specify the path for the CGI program, as well. Using the date.cgi program, you accomplish the same thing by embedding the following line in your html document:
You need to keep a few things in mind when you are including the output of your CGI programs in your HTML documents. First, the server does not check to make sure the CGI program returns HTML. If your CGI program returns an image, you will see junk on your screen. Second, you cannot pass parameters to your CGI program over the command line. Finally, notice that including CGI output is extremely inefficient. First, the server accesses the document, which it then parses. Next, the server runs a CGI program. Finally, it sends the output of the whole thing to the browser.
On a heavily loaded server, you are not going to get good performance if you include CGI output in your HTML documents.
Note that this increment function and consequently the entire counter applicationhas limited functionality. The counter data file is hard coded in the program and can consequently be used only with one corresponding HTML document. A more sophisticated counter program would enable you to define where the counter data is located. Additionally, the increment function is not extremely robust because it assumes that the counter.data file contains only one number. It would be nice (but not necessary) to add some error routines.
Domain Name Search
|