Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server."— Presentation transcript:

1 CGI Common Gateway Interface

2 CGI is the scheme to interface other programs to the Web Server.

3 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

4 How to communicate with a CGI Script? using a hyper link using FORMs

5 Sending info to the CGI Script A CGI (Common Gateway Interface) 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.

6 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 In [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.

7 ACTION ACTION: or, 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”, slackware 8.0: “var/www/cgi-bin”.

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

9 Facilitates Information TransferWeb Server Acts directly Upon the Data Two possible functions or roles that a CGI can fulfill Data

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

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

12 Languages of CGI Scripts CGI Scripts can be written in a variety of different computer languages: C/C++ Perl the UNIX shell TCL (Tool Command Language) Visual Basic

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

14 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. e.g., slackware 8.0 installation: “var/www/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.

15 cgi-bin cgi-bin directory is located on linux system with default installation of Apache server: /var/lib/apache/cgi-bin /var/www/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

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

17 Running a cgi script So, for example, imagine that you type the following URL into your browser: http://www.howstuffworks.com/cgi-bin/search.pl The server recognized that search.pl is in the cgi-bin directory, so it executes search.pl (which is a PERL script) and sends the output from the execution to your browser. That is how the search engine for How Stuff Works is implemented.

18 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!

19 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; }

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

21 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/~brasheed/cgi-bin/simplest.cgi As you can see, all that the script does is generate a page that says, "Hello there!".

22 PERL script print "Content-type: text/html\n\n"; print " Hello World! \n";

23 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";

24 Lab exercise 1. Go to the following site and implement all the example cgi programs on your server. How CGI Scripting Works by Marshall Brain http://www.howstuffworks.com 2. Write a cgi program(C/C++) and a script (Perl) when executed will send the following message to the browser: “This is my first cgi script.” 3. 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.

25 Test your CGI scripts on Apache Server running on csplinux telnet to csplinux.saultc.on.ca –Create a directory (if does not exist) called public_html in your home directory: 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://csplinux.saultc.on.ca/~username/cgi-bin/firstperl.cgi

26


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

Similar presentations


Ads by Google