Monday, December 8, 2008

CGI environment variables

When a CGI script is called several environment variables are set and you can access these from Perl. To see what some of the environment variables are you can try the example perl CGI script at

http://www.comp.leeds.ac.uk/cgi-bin/Perl/environment-example

This program simply accesses Perl's associative %ENV array to print out the values of the environment variables. You can have a look at the source of this program to see how it accesses the variables.

Possibly the most useful of these environment variables is the QUERY_STRING variable. In the above example this was the empty string, but if we append, say,

?something

onto the end of the URL then the string after the ? gets put into the environment variable QUERY_STRING. Try opening the following URLs. The first one is the same as we used above.
http://www.comp.leeds.ac.uk/cgi-bin/Perl/environment-example http://www.comp.leeds.ac.uk/cgi-bin/Perl/environment-example?something
http://www.comp.leeds.ac.uk/cgi-bin/Perl/environment-example?1
http://www.comp.leeds.ac.uk/cgi-bin/Perl/environment-example?2
http://www.comp.leeds.ac.uk/cgi-bin/Perl/environment-example?two+words

A simple CGI script

The first task is to write a very simple perl program which just prints out an HTML document. This should include all the usual '' ... '' and '' ... commands. However, there are two things to note.(pl ingore '' sign)

First of all, the first two lines of the print out are special. The first line must be

Content-type: text/html

so that the browser knows what kind of document it is and therefore how to display it. In this case it is a text document of the subclass HTML. You can also send back plain ASCII documents by using text/plain instead. Sound samples and images also have to have the content type specified appropriately. The second line must be blank (ie it must contain just a line feed). This line is reserved for future development.

The second point is really a tip: Strings in perl programs can span over several lines. This means the main part of your perl program can just be one enormous print statement.

original info from
http://www.comp.leeds.ac.uk/Perl/Cgi/simple.html

What CGI scripts are

Normally when a Web browser looks up a URL the following happens. First your computer contacts the HTTP server with the URL. The HTTP server looks at the filename requested by your computer and then sends that file back. Your computer then displays the file in the appropriate format.

However, it is possible to set up the HTTP server so that whenever a file in a certain directory is requested that file is not sent back; instead it is executed as a program, and whatever that program outputs is sent back for your computer to display. This function is called the Common Gateway Interface or CGI. The programs are called CGI scripts. The directory that the CGI scripts sit in cannot be specified by you alone - you need the help of whoever runs your HTTP server.

In the School of Computer Studies here at Leeds it's Support who run the HTTP server. If you have an account here and you ask Support to give you a Web page and a CGI directory they tell the HTTP server where to look for your Web files. For example, if your username is sam then your Web pages can be put in directory /home/www/sam and your CGI scripts in directory /home/www/cgi-bin/sam. When Support are told this they make sure that when someone sends URL

http://www.comp.leeds.ac.uk/sam/filename

then the file /home/www/sam/filename is sent back. And when they send the URL

http://www.comp.leeds.ac.uk/cgi-bin/sam/filename

then the file /home/www/cgi-bin/sam/filename is executed. In all cases you have to set the permissions of the files and directories so that everyone can read them. You also have to set the permissions of all the directories and the CGI scripts so that they are executable by everyone.

In summary CGI scripts are programs which can generate and send back anything: sound, pictures, HTML documents, and so on. In this tutorial we concentrate on generating and sending back HTML documents.

What is CGI?

"CGI" stands for "Common Gateway Interface." CGI is one method by which a web server can obtain data from (or send data to) databases, documents, and other programs, and present that data to viewers via the web. More simply, a CGI is a program intended to be run on the web. A CGI program can be written in any programming language, but Perl is one of the most popular, and for this book, Perl is the language we'll be using.

CGI is the Common Gateway Interface which allows you to create Web pages on the fly based on information from buttons, checkboxes, text input and so on. The pages can be images, sounds, text and indeed everything else transferable by the Web. They can even be references to other Web pages. In this tutorial we concentrate on creating HTML documents (rather than images and sounds) but the principles transfer readily to other formats. This should provide a good start to cope with the more comprehensive documentation elsewhere. For example, you should be able to find your way around the CGI programmer's reference with greater purpose, and perhaps some of the CGI material on Yahoo.

Lasso, SMX, Ruby on Rails

The following three web application frameworks are what we call up an coming scripting languages. Ruby On Rails is a framework based on the Ruby programming language with a goal to make the production of database reliable websites much faster. Ruby On Rails are used by many online businesses and although their services may be offered by web hosting businesses it is still to be considered a small player in the game of scripting. SMX is referred to as a macro processing language that has been around since day one. Currently in the shadow of PHP and ASP, SMX is the perfect script language to use for implementation of web applications in HTML/XML pages. When it comes to Lasso, this programming language is primary known to, like the above, enable the developer to create dynamic websites. Similar to other scripting languages like PHP, Perl and Python, Lasso can run their servers on Mac OS X, Windows 00-03 and Red Hat Linux.

CGI (Common Gateway interface) Scripts

Based on executable .cgi files stored in the cgi-bin directory on your web server users can implement applications and features like visit counters and clocks on desired web pages. By adding a line of common gateway interface code into the web page HTML code it will send a request to execute desired action when the web browser is loading that web page. Web Hosts supporting cPanel offer already set CGI-scripts for download or installation. Common CGI scripts used are FormMail, to implement a email contact form on a web page.
CGI Scripts have the reputation of being quite slow leaving the visitors waiting a long time for the page to display properly.

ASP.NET

This scripting language is the offspring of the classic ASP (Active Server Page technology) and the Microsoft's .NET framework. The new version decreases the code density and although there are less web servers supporting ASP.NET than PHP this scripting language has the same potential. With an ASP.NET file ending in .aspx the script is executed, unlike HTML, by the server, not web browser. And as this scripting language is basically the same as PHP you can implement its dynamic code into your html file.
Make sure that the web host server supports ASP.NET by either simply asking or finding out if the server runs on Windows operative system since this scripting language comes from Microsoft.