separator

Take a quiz or make one of your own
Tutorial 2 - Counting Web Page Hits with SSI
Installation:
Part I: Server side installation:
  1. ftp article02.cgi to your cgi-bin/thecgibin directory
  2. on telnet change directories to cgi-bin/thecgibin and enter the command 'perl -wc article02.cgi'.
    • you should get the response 'article02.cgi syntax ok'


  3. now that the program's on your server and compiles cleanly, you need to set up the data file. use the 'data' subdirectory of thecgibin.
    • create a new file on your text editor and type the number 0 on it and save the file as article02.dat
    • now ftp article02.dat into cgi-bin/thecgibin/data
    • on telnet say 'perl -wT article02.cgi'
    • now look at the contents of cgi-bin/thecgibin/data/article02.dat. one way would be to view the file through your ftp client on the server... you could even download it from your server to your computer and then look in your text editor. if you ran the program once it should equal '1', twice should be '2'.


Part II - installation on the web:
Now here comes the meat of this article. So far all we've done is run our program from the command line and update the count file. Really no different at all from the previous tutorial. In order to make this program work for the web we're going to use the SSI facility, i.e. we'll make it a server side include.
You'll have to be on a server that not only allows you to do Perl programming but also has SSI enabled. Assuming you've already checked that out, let's proceed. We need a webpage, and to let the server know it's going to use SSI we'll give it a file extension of SHTML.
I wrote a simple one below that you can copy:


Hello Out There




Hello Out There




Installing your Webpage:
  1. in your text editor copy the html code above into a file on your local computer and call it article02.shtml
  2. ftp article02.shtml to your cgi-bin/thecgibin directory
  3. now get on a browser window and type in the url http://www.mysitename/cgi-bin/thecgibin/article02.shtml/
  4. now go to ftp and look at the data/article02.dat file. it didn't change, did it? that's because we haven't changed the chmod yet.
Setting File Permissions:
On your ftp client there will be some mechanism for changing permissions, aka chmod's on the server. The chmod describes who can do what with any of your files. It is a three by three matrix and should look something like this:
                             owner       group       public
           read
           write
           execute/list
                
When you ftp'd your files you did it after signing on to the server with a password, which indicated that you were the file owner. But anyone at all can get to your files from the web, if you permit them. Which is after all the beauty of the internet, right? Well, only if you want them to.

That's where the chmod's come in.

Let's review what we've done so far. We've written a Perl program, a data file, and a web page, i.e. an HTML file to which we've given the extension of SHTML. Why? So that the server knows our webpage contains a server side include or SSI which it will execute before rendering the webpage to the client's browser.
Now those are the files. How do they work together, and what permissions are needed? When we're browsing on the web, from that website's server's point of view we are in the public column of the chmod matrix. Makes sense, doesn't it? Whereas when we run from the command line, i.e. telnet, we are in the owner column of the matrix. Because telnet like ftp will require us to log on with a userid and password... that's how it knows we're the owner.
Have you figured it out yet? In order to execute our article02.cgi program as an SSI from our article02.shtml webpage we have to change the chmod so it can be executed by the public. The appropriate chmod would be 755 which means check off the boxes for read and execute on all columns and also for writing for the owner column.
Now, let's get back to that webpage. Refresh/reload it and see what you get. Here's what I got:
cannot open count : Permission deniedBad file descriptorcannot close
count : Bad file descriptor

Hello Out There
Huh?
What happened now? If you haven't gotten frustrated yet then you have a lot more patience than I did when I installed my first count program on the web. First of all let's see where that error message came from.
Could it be.... the open and close statements in our program?
Yep, that's it alright. Let's look at those lines again. Back in our program we said:
print 'cannot open count : ',$! # ready count file
if not open (COUNT, '+
AND
print 'cannot close count : ',$!
if not close (COUNT); # done working with count file
And lo and behold, that's what our webpage says, followed by the 'Hello Out There' line. Except what's that other stuff on those error messages? Those are the system error descriptions coming from where we printed out the variable '$!'. Perl's full of shorthand builtin variables like $!.
Since the chmod was never set properly for the data file our program wants to write to, the server told us it was denying permission to open the file for output. Because when running from a webpage we are executing the program as public, right?
Furthermore since the program couldn't open the file it couldn't close it later either because there was no open file to close. So now we'll fix this problem by going to the ftp client and changing the permissions for data/article02.dat to 666 which means read and write is enabled for all columns.
Now back to the browser and refresh the webpage. It should just say 'Hello Out There' and nothing else. If so then reload the page a few more times. Ok? Now go to the ftp client and have a look at that data file on your server. I've lost count by now myself but it should have added one for each refresh you made of the page on your browser.

Pretty cool, huh?

separator

© www.thecgibin.com