home -- outline -- lectures -- assignments -- discussion -- tips -- links -- | |
![]() ![]() |
|
![]() |
CGI & PerlThese protocols, called the "Common Gateway Interface" or "CGI", allow programs to be called from web pages directly, using either FORMS or A tags. They determine how information is handed to programs from web pages and how those programs have to respond. Most Web servers impose strong limits on what can and can not be done from the Web. Programs (and often pages) are sometimes locked behind password protections that restrict access to a collection of pages. In doing CGI programming there are even greater restrictions. For most sites, CGI code must be placed in certain directories for it to even run on the Web. On the CS department's research machines, CGI programs have to be placed in a special directory called cgi-bin. Any code not in that directory is simply not run. On the CLASSES server, each student can run CGI directly from their html directory. Any code that is in that directory and has the "cgi" suffix can potentially be run from the Web. Code anywhere else can not be. For example, there is a Perl script in the cgi area for this class in CS101/html. It is called test8.cgi. The exact same piece of code, sitting in Kris' html directory on the CS research machines gives very different result: It is called test8.cgi. The location of the code is not enough, however. The "permissions" on the code have to set so that anyone can run it. This is done on UNIX machines (which the classes server is) by using a command called chmod. chmod sets the permissions on a files telling the system who can read, write and execute it. To make a file publicly executable, we call chmod with the arguments "a+x" and the name of the file. For example, if we have a file "test8.cgi" that we would like to have run from the Web, we would have to invoke chmod by typing: The "a" means "all" and the "x" means "execute". When a program is not set to be publicly executable, we get the familiar Forbidden message. Of course, if the program is faulty, we get a more telling and embarrassing message. If you try to run the same program from the UNIX side, you tend to get a more useful message... [12:13am] kris@yeenoghu:html 43% perl test7.cgi syntax error at test7.cgi line 16, near "print" Execution of test7.cgi aborted due to compilation errors.Which tells us that we made a syntax error on line 16 of our code: #! /opt/local/bin/perl use CGI qw(:standard); import CGI font; print "Content-type: text/html\n\n"; print "< HTML>"; print "< head> < TITLE> $first_name Page< /TITLE> < /head>\n"; print "< body bgcolor=lightblue text=#000000 link=#006633\n"; print " alink=#996600 vlink=#663300\n>"; print "< br> < center> < h3> I am CGI run from an html directory."; print "< /h3> < /center>< br>\n" print "< /HTML>";Though, once the permissions are set, the code is in the right place, and it is debugged, it will work great. As we discussed last week, the FORM tag and the variations on the INPUT tags are used to send information to a program. That program then has to produce a page in response. In general, that page is an html page, but it can be any mime type that the browser knows of, as long as it is identified as such. Fortunately, anything that is printed to standard input from a program invoked on the Web will be sent back as the contents of the page. This makes it very easy to build pages by simply printing. To identify a page as an html page, the line: followed by two carriage returns has to precede anything else that is printed. If not, the text will be treated as just that, text. In Perl, the scripting language you will be using, this is done with the following print statement: Note that at the end of the statement, there is a ";". This tells Perl that the expression is finished. The problem we had earlier was a result of a lack of ";". This will be your most common problem. Once we have the content type information out, anything we print from the program will have the same effect as typing it into a file. So a lot of CGI tends to look like... #! /opt/local/bin/perl use CGI qw(:standard); import CGI font; $title=¶m('title'); $back=¶m('back'); $font=¶m('font'); $bold=¶m('bold'); $size=¶m('size'); print "Content-type: text/html\n\n"; print "< HTML>"; print "< head> < TITLE> $title < /TITLE> < /head>\n"; print "< body bgcolor=$back text=$font link=#006633\n"; print "alink=#996600 vlink=#663300\n>"; if ($bold){print "< b>"}; if ($size){print "< font size=+4>"}; print "< center> This page is called: < br>$title< br>< br>"; print "This is a seriously boring page, "; print "but it does change colors and style."; print "< /center>< br>< h3>< a href=test7.txt>"; print "Source< /a>< /h3>< br>\n"; print "< /body>< /HTML>";This was the page from Friday that let us build a new page with various colors, fonts and styles. Note the use of "IF" here. What happens if you want to not just print a page, but save some information about someone's interaction with your page? Well, we can write that information to a file and save it for later. For example, we can record the name of everyone who uses a form by diverting the information they give us to a file. Recall the FORM example we used last week. We can change it to not only spit back someone's name to a page but also build up a list of people who have used it.
But what if we want to let people see the list of visitors to our little page? How do we get to this file and show it. Well, here we need to build a script that reads a file in rather than just writes it. Fortunately, this is easy as well. We can even call the script directly. Frighteningly enough, you now have most of what you need to do the assignment for this week. |