This was also a good lab for several years, hence, I will reuse it.

Adding a counter to a web page

This is not going to be easy for either of us. But perhaps it will be instructive.

The HTML and an IFRAME

Make a web page that has "This page has been accessed: <iframe src=counter.cgi></iframe> times"

Put this on the siesta server in your folder (log in as cs160, password is ..., and cd to .www-docs, then cd to your own directory if you have one; if not, go into ssh file transfer and create one in the right place). Let's call this yourname.html. Verify that you can read this page from the browser (http://www.cs.wustl.edu/~cs160/yourname/yourname.html). The counter.cgi should fail. You can put a width and height on the iframe if you want. You can also control the scrolling with scrolling=no.

Now, add the following file to your folder on siesta, under the name counter.cgi. You actually won't be able to create it in notepad or wordpad and use ssh file transfer to move it over, since microsoft products insist on putting the control-M character at the end of each line, which causes UNIX programs to barf. You could use pico to create the file on the server, if you don't want the TA to show vi.


#! /pkg/gnu/bin/gawk -f
BEGIN {
  print "Content-type: text/html\n"
  print 12
}

And make it public-executable and public-readable. Try refreshing your page now, and see that your page has been accessed twelve times.

Or you could use this perl program, since perl ignores all the control-M's that dos generates.


#! /usr/bin/perl
print "Content-type: text/html\n\n";
print 12

Is this satisfactory?

Now you really want to try something like:


#! /pkg/gnu/bin/gawk -f
BEGIN {
  getline n < "counter.txt"
  close("counter.txt")
  print "Content-type: text/html\n"
  print n
  print n > "counter.txt"
}

or

#! /usr/bin/perl
print "Content-type: text/html\n\n";
open myfile,"counter.txt";
$n = <myfile>;
print $n;
close myfile;
open myfile,'>',"counter.txt";
print myfile $n

which would save the last counter value on your folder in the file counter.txt. Remember to make your file counter.txt both public-readable and public-writeable. You'll have to make a counter.txt file with a single number in it and ssh-file-transfer it to the server.

There's something still wrong with this script. Can you tell me what it is?

You can fix it either by putting "n=n+1" or "n++" or "++n" in exactly the right place. Perhaps you can reason about the program and figure out where this should go. Ask the TA for help and for an explanation of the differences between the three.

That's not bad, is it?

The IMAGE

Unfortunately, this is not how people usually do this. What they normally do is use an image instead of an iframe.

Replace your IFRAME with the tag <IMG SRC=counter.cgi>.

The problem is that you need to write a gif or jpg instead of plain text. Try this as your counter.cgi.


#! /pkg/gnu/bin/gawk -f
BEGIN {
  getline n < "counter.txt"
  close("counter.txt")
  print "Content-type: image/gif\n"
  n++
  print n > "counter.txt"
  if (n%5 == 1) system("cat one.gif")
  if (n%5 == 2) system("cat two.gif")
  if (n%5 == 3) system("cat three.gif")
  if (n%5 == 4) system("cat four.gif")
  if (n%5 == 0) system("cat five.gif")
}

or

#! /usr/bin/perl
print "Content-type: image/gif\n\n";
open myfile,"counter.txt";
$n = <myfile>;
close myfile;
open myfile,'>',"counter.txt";
$n++;
print myfile $n;

if ($n%5 == 1) { system "cat one.gif" }
if ($n%5 == 2) { system "cat two.gif" }
if ($n%5 == 3) { system "cat three.gif" }
if ($n%5 == 4) { system "cat four.gif" }
if ($n%5 == 0) { system "cat five.gif" }

Go on the web and find five images, preferably pictures of numerals, and save them as one.gif, etc. Put them in your siesta folder and make sure they are public-readable. I actually had some fun finding them on images.google.com, searching just for 'one', 'two', etc. Make sure you save gifs, not jpgs. And make sure you save the images, not the pages that refer to the images.

Try this script now. Did it work? (Keep fingers crossed.)

What do you suppose would have to be done for the counter to go above five?

Go back to your page which has the text "has been accessed ... times" and see if it now works. If your images are too big, you can put a height and width attribute on your iframe tag, e.g.,