CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

34
CGI Common Gateway Interface

Transcript of CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

Page 1: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

CGI

Common Gateway Interface

Page 2: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

CGI

• is the scheme to interface other programs to the Web Server.

Page 3: 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

Page 4: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

How to communicate with a CGI Script?

• using a hyper link

• using FORMs

Page 5: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

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.

Page 6: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

Using Link

<html>

<head>

</head>

<body>

<h1> A simple CGI script </h1>

<p>

<a href="http://csplinux.saultc.on.ca/~brasheed/cgi-bin/firstperl.cgi">

Click here to see the first perl CGI script</a>

using absolute link.

</p>

</body>

</html>

Page 7: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

firstperl.cgi

#!/usr/bin/perl

print "Content-type: text/html\n\n";

print "<html><body><h1>Hello World!</h1></body></html>\n";

Page 8: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

Sending info to the CGI Script<FORM METHOD=POST ACTION="http://csplinux.saultc.on.ca/

cgi-bin/simpleform.cgi">

• METHOD: POST vs. GETThere 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.

Page 9: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

METHOD=GET

<html>

<head>

</head>

<body>

<h1> A super-simple form </h1>

<FORM METHOD=GET ACTION="http://csplinux.saultc.on.ca/~brasheed/cgi-bin/simpleform.cgi">

Enter Your Name: <input name="Name" size=20 maxlength=50>

<p> <INPUT TYPE=submit value="Submit"> <INPUT TYPE=reset value="Reset">

</FORM>

</body>

</html>

Page 10: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

simpleform.cgi

#!/usr/bin/perl

print "content-type: text/html\n\n";

print("<html>");print("<head>");print("</head>");print("<body>");print("<h1>");print "The value entered was: ";print $ENV{'QUERY_STRING'};print "<br>";print("</h1>");print("</body>");print("</html>");

Page 11: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

METHOD=GET is the default method

<html>

<head>

</head>

<body>

<h1> A super-simple form </h1>

<FORM ACTION="http://csplinux.saultc.on.ca/~brasheed/cgi-bin/simpleform.cgi">

Enter Your Name: <input name="Name" size=20 maxlength=50>

<p> <INPUT TYPE=submit value="Submit"> <INPUT TYPE=reset value="Reset">

</FORM>

</body>

</html>

Page 12: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

METHOD=POST (standard input)

<html>

<head></head>

<body><h1> Asuper-simple form </h1>

<FORM METHOD=POST ACTION="http://csplinux.saultc.on.ca/~brasheed/cgi-bin/simpleform_post.cgi">Enter Your Name: <input name="tbox" size=20 maxlength=50><p> <INPUT TYPE=submit value="Submit"> <INPUT TYPE=reset value="Reset"></FORM>

</body>

</html>

Page 13: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

METHOD=POST (standard input)

#!/usr/bin/perl

print "content-type: text/html\n\n";use CGI qw(:standard);$tboxcontent = param( "tbox" );

print("<html>");print("<head>");print("</head>");print("<body>");print("<h1>");print "The value entered was: ";print $tboxcontent;#print $ENV{'QUERY_STRING'};print "<br>";print("</h1>");print("</body>");print("</html>");

Page 14: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

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”.

Page 15: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

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.

Page 16: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

• Facilitates Information Transfer Web Server

• Acts directly Upon the Data

Two possible functions or roles that a CGI can fulfill

Data

Data

Page 17: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

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.

Page 18: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

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.

Page 19: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

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

Page 20: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

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.

Page 21: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

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.

Page 22: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

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

Page 23: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

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.

Page 24: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

Running a cgi script

• Type the following URL into your browser:

http://csplinux.saultc.on.ca/cgi-bin/simpleform.cgi

• 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.

Page 25: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

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:

<html> <body> <h1>Hello there!</h1> </body></html>

Page 26: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

CGI program wrote in C++

• The simplest possible CGI script would, upon execution, create this simple, static page as its output.

#include <iostream.h>int main(){ cout << "Content-type: text/html\n\n"; cout << "<html>\n"; cout << "<body>\n"; cout << "<h1>Hello there!</h1>\n"; cout << "</body>\n"; cout << "</html>\n"; return 0;}

Page 27: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

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.

Page 28: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

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: http://csplinux.saultc.on.ca/cgi-bin/simplest.cgi. As you can see, all that the script does is generate a page that says, "Hello there!".

Page 29: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

PERL script

print "Content-type: text/html\n\n";

print "<html><body><h1>Hello World!</h1></body></html>\n";

Page 30: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

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 "<html><body><h1>Hello World!</h1></body></html>\n";

Page 31: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

Practice

• Go to the following site and implement all the example cgi programs on your server.

• How CGI Scripting Worksby Marshall Brain

http://www.howstuffworks.com

• 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.

Page 32: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

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.

http://eagle.auc.ca/~username/cgi-bin/firstperl.cgi

Page 33: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

CGI Environment Variables• http://hoohoo.ncsa.uiuc.edu/cgi/env.html

• CGI- Does it stand for Can’t Get It-to-work?

• http://mkruse.netexpress.net/info/cgi/

Page 34: CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.

SSI• http://hoohoo.ncsa.uiuc.edu/docs/tutorials/includes.html• http://www.carleton.ca/~dmcfet/html/ssi1.html• http://www.useforesite.com/tut_ssi.shtml