Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lesson 11. CGI CGI is the interface between a Web page or browser and a Web server that is running a certain program/script. The CGI (Common Gateway Interface)

Similar presentations


Presentation on theme: "Lesson 11. CGI CGI is the interface between a Web page or browser and a Web server that is running a certain program/script. The CGI (Common Gateway Interface)"— Presentation transcript:

1 Lesson 11

2 CGI CGI is the interface between a Web page or browser and a Web server that is running a certain program/script. The CGI (Common Gateway Interface) is a way for a request to a web server to cause a program to execute, instead of just causing a static HTML page to be returned. The program that is run then dynamically generates a web page tag by tag, which is sent back to the browser that made the request.

3 CGI Perl is extremely powerful and well-suited to web programming, and is the language usually used with the CGI.

4 CGI A typical example would work like this; 1.The web user clicks the submit button of a simple form in which they have entered their first and last name. 2.The tag uses the get method, and the action http://alexis.senecac.on.ca/~in222h01/cgi-bin/script1.pl

5 CGI  The form information is added to the URL as follows; http://alexis.senecac.on.ca/~in222/cgi-bin/script1.pl?fname=joe&fname=smith  The http request is sent to the server.  Because the desired file ends with the.pl extension and is in the cgi-bin directory, the CGI causes the Perl interpreter to run the program called script1.pl.  When the CGI does this, it makes the form information (fname and lname in this case) available to the program as it executes

6 The browser contacts the server and requests a document. The server translates the URL into a path and file name. If the server finds out that the URL points to a program then –It prepares the environment and then runs the script. –The script executes and reads the environment variables and STDIN. –The script sends the proper headers to STDOUT for the forthcoming content and then it sends the rest of its output to STDOUT and terminates. –The server notices that the script has finished and closes the connection to your browser. Your browser displays the output from the script..

7 HTTP Request When an HTTP request is sent to a browser, it contains more information than just the details of the request.

8 Environment variables When CGI receives a request from a browser, it also gets a lot of other information. It makes this information available to your Perl programs in the form of a hash named %ENV.

9 Displaying the environment We can use a simple script to display the %ENV contents for our particular server, as shown by the following example

10 Get vs. Post Let’s take another look at the difference between get and post, this time from the server side.

11 Get We know that using get to submit a form appends the form contents to the url. This data is provided for us in the %ENV hash, under the key $QUERY_STRING.

12 Post On the other hand, the data sent by the post method is received via stdin, and must be processed differently

13 Handling post data The following example will show how we can obtain the data sent by the post method, and send it back to the user

14 Handling post data The first thing we have to do is get the form data from stdin. read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); STDIN is a handle that refers to standard input. $buffer is an arbitrary variable that will hold the entire input string. CONTENT_LENGTH is the environmental value that lists how many bytes are being input.

15 Handling post data Once we have the input data in the variable $buffer, we can process it the same way that we processed the data in the previous example, using the split function to separate the parts of the string

16 get vs. post 1.#!/usr/bin/perl 2.print "Content-type:text/html\n\n"; 3.print " Form Output "; 4.print " Results from get method \n"; 5.print " "; 6.@values = split(/&/,$ENV{'QUERY_STRING'}); 7.foreach $i (@values) { 8. ($varname, $mydata) = split(/=/,$i); 9. print " "; 10. $mydata =~ tr/+/ /; 11. $mydata =~ s/%([a-fA-F0-9][a-fA- F0-9])/pack("C", hex($1))/eg; 12. print "$varname = $mydata\n"; 13. print " "; 14.} 15.print " "; 16.print " "; 1.#!/usr/bin/perl 2.print "Content-type:text/html\n\n"; 3.read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); 4.@data= split(/&/, $buffer); 5.print " Form Output "; 6.print " Using post method \n"; 7.print " "; 8.foreach $i (@data) { 9.($name, $value) = split(/=/, $i); 10.$value =~ tr/+/ /; 11.$value =~ s/%([a-fA-F0-9][a-fA-F 0-9])/pack("C", hex($1))/eg; 12.print " "; 13.print $name, " = ", $value; 14.print " "; 15.} 16.print " "; 17.print " ";


Download ppt "Lesson 11. CGI CGI is the interface between a Web page or browser and a Web server that is running a certain program/script. The CGI (Common Gateway Interface)"

Similar presentations


Ads by Google