CGI Common Gateway Interface
CGI is the scheme to interface other programs to the Web Server.
CGI provides features on Web page Generating Interactive Pages –Shows current date, get server’s IP Processing Forms –Serach a database, search engines –Dynamic Pages from material in a database
How to communicate with a CGI Script? using a hyper link using FORMs
Sending info to the CGI Script A CGI Script is any program or set of commands running on the Web server that receives data from the Web page and then acts on that data to perform a certain task.
Using Link A simple CGI script Click here to see the first perl CGI script using absolute link.
firstperl.cgi #!/usr/bin/perl print "Content-type: text/html\n\n"; print " Hello World! \n";
Sending info to the CGI Script METHOD: POST vs. GET There are two methods of sending information to the CGI from the HTML form: –POST:where the user-input information is placed in the 'body' of the message (Technically, within Standard Input [STDIN]) and –GET:where the user-input information is appended to the URL that the information is sent to. With POST, the designer can design the form to accept a great deal of user- supplied information whereas with GET, because all the information is being appended to an already long URL, the amount of transferable information is limited. For most purposes, it is best to design your forms to send using a POST. If there are a very few possible fields, such as a three-word database query, GET would be suitable.
METHOD=GET A super-simple form Enter Your Name:
simpleform.cgi #!/usr/bin/perl print "content-type: text/html\n\n"; print(" "); print "The value entered was: "; print $ENV{'QUERY_STRING'}; print " "; print(" ");
METHOD=GET is the default method A super-simple form Enter Your Name:
METHOD=POST (standard input) Asuper-simple form Enter Your Name:
METHOD=POST (standard input) #!/usr/bin/perl print "content-type: text/html\n\n"; use CGI qw(:standard); $tboxcontent = param( "tbox" ); print(" "); print "The value entered was: "; print $tboxcontent; #print $ENV{'QUERY_STRING'}; print " "; print(" ");
ACTION ACTION: for most CGIs, 'where to send the info' The ACTION parameter of the HTML form declaration tells the server where to send the information, or where the server is to look for a process that can deal with the information it has received. It is in the form of a standard UNIX path. The path is usually something like “usr/bin/cgi-bin” or “var/lib/apache/cgi-bin”.
Two Roles of CGI There are two possible functions or roles that a CGI can fulfill, that of the information pipe and that of the processor itself. Facilitates Information Transfer –CGI in its most common role, acting as a facilitator, for example, a 'mail-back form processor' Acts directly Upon the Data –A CGI can act directly on the data, for example, a counter script or WAISPERL, a PERL 5 extension that is capable of creating a query without the aid of a WAIS (Wide Area Information Service) Server.
Facilitates Information TransferWeb Server Acts directly Upon the Data Two possible functions or roles that a CGI can fulfill Data
Access to a CGI SCript Because CGI Script run on the Web Server, you, as a Web page designer/author, might not have the ability to create or edit them. So, you might need the help of the system administrator.
Disadvantages of CGI Users had to be connected to the Web server to run the CGI script; only the programmer could alter the script itself; the system administrator of the Web server could place limitations on how users accessed the script, and so on. Also posed problems for the system administrator, who had to be concerned about users continually accessing the server, slowing it down, and overloading the system.
Languages of CGI Scripts CGI Scripts/Programs can be written in a variety of different computer languages: C/C++ Perl (Practical Extraction and Reporting Language) PHP (Personal Home Page tools/PHP Hypertext Processor) the UNIX shell TCL (Tool Command Language) Visual Basic
Solution Client-side programs solve many of the problems associated with CGI scripts. However, Client-side programs can never completely replace CGI Scripts. e.g., search engines.
Where CGI scripts are stored ? On most web servers, the CGI mechanism has been standardized in the following way. In the normal directory tree that the server considers to be the root you create a subdirectory named cgi-bin. The server then understands that any file requested from the special cgi-bin directory should not simply be read and sent, but instead should be executed. The output of the executed program is what it actually sent to the browser that requested the page. The executable is generally either a pure executable, like the output of a C compiler, or it is a PERL script. PERL is an extremely popular language for CGI scripting.
cgi-bin cgi-bin directory is located on linux system with default installation of Apache server: /var/lib/apache/cgi-bin cgi-bin directory is located on Windows system with Xitami server installed: C:\Xitami\cgi-bin cgi-bin directory is located on Windows system with Personal Web Server (PWS) server installed: C:\Inetpub\wwwroot\cgi-bin
cgi-bin directory for your Lab Exercise You need to create a directory called cgi-bin in the directory public_html located in your home directory: ~/public_html/cgi-bin/ All CGI scripts must have a.cgi extension and be executable.
Running a cgi script Type the following URL into your browser: The server recognized that simpleform.cgi is in the cgi-bin directory, so it executes simpleform.cgi (which is a PERL script) and sends the output from the execution to your browser.
Simple CGI Scripts Assuming that you have access to a cgi-bin directory, and assuming that you know either the C programming language or PERL, you can do a whole bunch of interesting experiments with CGI. Let's start by creating the simplest possible CGI script. The simplest possible HTML web page looks something like this: Hello there!
CGI program wrote in C++ The simplest possible CGI script would, upon execution, create this simple, static page as its output. #include int main() { cout << "Content-type: text/html\n\n"; cout \n"; cout Hello there! \n"; cout \n"; return 0; }
cout << "Content-type: text/html\n\n"; The line "Content-type: text/html\n\n" is special piece of text that must be the first thing sent to the browser by any CGI script. If you forget, the browser will reject the output of the script.
Compiling a CGI program On my web server, I have entered this program into the file simplest.cpp and then compiled it using Visual C++ compiler. On csplinux: gcc simplest.cpp -o simplest.cgi By placing simplest.cgi in the cgi-bin directory it can be executed. You can try it out now if you like by typing in or clicking on this URL: bin/simplest.cgi. As you can see, all that the script does is generate a page that says, "Hello there!".
PERL script print "Content-type: text/html\n\n"; print " Hello World! \n";
PERL script for Apache on Linux and Xitami on Windows: #!/usr/bin/perl # The above line is pointing where the Perl interpreter is. print "Content-type: text/html\n\n"; print " Hello World! \n";
Practice Go to the following site and implement all the example cgi programs on your server. How CGI Scripting Works by Marshall Brain Also write a cgi program that will take a name from a form and send you back on a dynamic Web page with server name and IP address.
Test your CGI scripts on Apache Server running on eagle telnet to eagle.auc.ca –Create a directory called public_html in your home directory (if does not exist): e.g., mkdir public_html –Change to the directory public_html : cd public_html –Create a directory called cgi-bin : mkdir cgi-bin Transfer your cgi scripts (e.g., firstperl.pl) to the cgi-bin directory using ws_ftp. Using your telnet session: –Rename your cgi scripts from.pl to.cgi e.g., mv firstperl.pl firstperl.cgi –Change file permissions to executable e.g., chmod 755 firstperl.cgi In your Browser’s address line type the following address and hit the Enter key.
CGI Environment Variables CGI- Does it stand for Can’t Get It-to-work?
SSI