Presentation is loading. Please wait.

Presentation is loading. Please wait.

Perl CGI What is "CGI"? Common Gateway Interface A means of running an executable program via the Web. Perl have a *very* nice interface to create CGI.

Similar presentations


Presentation on theme: "Perl CGI What is "CGI"? Common Gateway Interface A means of running an executable program via the Web. Perl have a *very* nice interface to create CGI."— Presentation transcript:

1

2 Perl CGI

3 What is "CGI"? Common Gateway Interface A means of running an executable program via the Web. Perl have a *very* nice interface to create CGI methods

4 CGI Architecture Diagram

5 How Does it Work? A program is created, like normal. The program must be made user-executable A user visits a web page. The web browser contacts the server and passes parameters to the program where the CGI program resides, and asks it to run the program The server runs the program with the parameters passed in The server takes the output of the program and returns it to the web browser. The web browser displays the output as an HTML page

6 Example 1 #!/bin/perl print "Content-type:text/html\r\n\r\n"; print ' '; print ' Hello Word - First CGI Program '; print ' '; print ' Hello Word! This is my first CGI program '; print ' '; 1; If you click hello.cgi then this produces following output: Hello Word! This is my first CGI program

7 HTTP Header The line Content-type:text/html\r\n\r\n is part of HTTP header which is sent to the browser to understand the content. Header Description Content-type: String A string defining the format of the file being returned. Example is Content-type:text/html Expires: Date String The date the information becomes invalid. This should be used by the browser to decide when a page needs to be refreshed. A valid date string should be in the format 01 Jan 1998 12:00:00 GMT. Location: URL String The URL that should be returned instead of the URL requested. You can use this to redirect a request to any file. Last-modified: String The date of last modification of the resource Content-length: String The length, in bytes, of the data being returned. The browser uses this value to report the estimated download time for a file Set-Cookie: String Set the cookie passed through the string

8 CGI Environment Variables Variable Name Description CONTENT_TYPE The data type of the content. Used when the client is sending attached content to the server. For example file upload etc CONTENT_LENGTH The length of the query information. It's available only for POST requests HTTP_COOKIE Return the set cookies in the form of key & value pair. HTTP_USER_AGENT The User-Agent request-header field contains information about the user agent originating the request. Its name of the web browser. PATH_INFO The path for the CGI script. QUERY_STRING The URL-encoded information that is sent with GET method request. REMOTE_ADDR The IP address of the remote host making the request. This can be useful for logging or for authentication purpose. REMOTE_HOST The fully qualified name of the host making the request. If this information is not available then REMOTE_ADDR can be used to get IR address. REQUEST_METHOD The method used to make the request. The most common methods are GET and POST. SCRIPT_FILENAME The full path to the CGI script SCRIPT_NAME The name of the CGI script. SERVER_NAME The server's hostname or IP Address SERVER_SOFTWARE The name and version of the software the server is running.

9 Running CGI Scripts The 2 most common ways of running a CGI script are: – From an HTML form – the “action” attribute of the form specifies the CGI script to be run – Direct URL reference – A CGI script can be run directly by giving the URL explicitly in HTML

10 Accepting Input from the Browser A CGI script can receive data in many ways: – Environment variables – Standard input Data can be passed as standard input through the POST method of an HTML form Example – Arguments of CGI script If you call a CGI script directly or use the GET method of a form, arguments are following the “?” after the script URL and multiple arguments are separated by &

11 GET vs. POST The GET method The GET method appends all of the form data to the end of the URL used to invoke the CGI script. A question mark is used to separate the original URL and the form information. htp:/somserver/cgi-bin/foo.cgi? fname=Craig &lname =Ke

12 The POST method The POST method sends all the form information to the CGI program using the STDIN file handle.

13 Example 2 Enter First and Last Name and then click submit button to see the result.The result is hello firstname lastname First Name: Last Name:

14 Example 2 hello_get.cgi #!/bin/perl if ($ENV{'REQUEST_METHOD'} eq "GET") { $buffer = $ENV{'QUERY_STRING'}; } @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $FORM{$name} = $value; } $first_name = $FORM{first_name}; $last_name = $FORM{last_name}; print "Content-type:text/html\r\n\r\n"; print " "; print " Hello - Second CGI Program "; print " "; print " Hello $first_name $last_name - Second CGI Program "; print " "; print " "; 1; http://local_host/cgi-bin/hello_get.cgi?first_name=ZARA&last_name=ALI

15 Simple FORM Example: GET Method First Name: Last Name:

16 Example 3 hello_post.cgi #!/bin/perl if ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); }else { $buffer = $ENV{'QUERY_STRING'}; } @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $FORM{$name} = $value; } $first_name = $FORM{first_name}; $last_name = $FORM{last_name}; print "Content-type:text/html\r\n\r\n"; print " "; print " Hello - Second CGI Program "; print " "; print " Hello $first_name $last_name - Second CGI Program "; print " "; print " "; 1;

17 Simple FORM Example: post Method - passes two values using HTML FORM and submit button First Name: Last Name:

18 Example 4 Passing Checkbox Data to CGI Program Maths Physics The result of this code is the following form Maths Physics

19 Passing Checkbox Data to CGI Program #!/bin/perl if ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); }else { $buffer = $ENV{'QUERY_STRING'}; } @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $FORM{$name} = $value; } if( $FORM{maths} ){ $maths_flag ="ON"; }else{ $maths_flag ="OFF"; } if( $FORM{physics} ){ $physics_flag ="ON"; }else{ $physics_flag ="OFF"; } print "Content-type:text/html\r\n\r\n"; print " "; print " "; print " Checkbox - Third CGI Program "; print " "; print " CheckBox Maths is : $maths_flag "; print " CheckBox Physics is : $physics_flag "; print " "; print " "; 1;

20 Passing Radio Button Data to CGI Program Radio Buttons are used when only one option is required to be selected. Here is example HTML code for a form with two radio button: Maths Physics

21 #!/bin/perl if ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); }else { $buffer = $ENV{'QUERY_STRING'}; } @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $FORM{$name} = $value; } $subject = $FORM{subject}; print "Content-type:text/html\r\n\r\n"; print " "; print " Radio - Fourth CGI Program "; print " "; print " Selected Subject is $subject "; print " "; 1;

22 Passing Text Area Data to CGI Program TEXTAREA element is used when multiline text has to be passed to the CGI Program. Here is example HTML code for a form with a TEXTAREA box: Type your text here...

23 #!/bin/perl if ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); }else { $buffer = $ENV{'QUERY_STRING'}; } # Split information into name/value pairs @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $FORM{$name} = $value; } $text_content = $FORM{textcontent}; print "Content-type:text/html\r\n\r\n"; print " "; print " Text Area - Fifth CGI Program "; print " "; print " Entered Text Content is $text_content "; print " "; 1;

24 Passing Drop Down Box Data to CGI Program Drop Down Box is used when we have many options available but only one or two will be selected. Here is example HTML code for a form with one drop down box Maths Physics

25 #!/bin/perl if ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); }else { $buffer = $ENV{'QUERY_STRING'}; } @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $FORM{$name} = $value; } $subject = $FORM{dropdown}; print "Content-type:text/html\r\n\r\n"; print " "; print " Dropdown Box - Sixth CGI Program "; print " "; print " Selected Subject is $subject "; print " "; 1;

26 CGI.pm This Perl 5 library uses objects to do many things. It –Uses objects to create Web fill-out forms on the fly and to parse their contents – Provides a simple interface for parsing and interpreting query strings passed to CGI scripts – Offers a rich set of functions for creating fill- out forms Everything is done through a “CGI” object

27 parameters submitted to your script. To do so, put the following at the top of your Perl/CGI programs: – Use CGI; – $query = new CGI; – Or Use CGI qw(:standard); CGI.pm gives you methods to create HTML code without actually writing HTML. most HTML tags are aliased to CGI functions.

28 Web application using PERL/CGI A simple adder Enter the first number Enter the second number

29 Html code Adder A simple adder Enter the first number Enter the second number

30 Add.cgi use CGI qw(param); read(STDIN, $data, ENV{"CONTENT_LENGTH"}); @field = split(/&/, $data); foreach $item (@field){ ($name, $value) = split(/=/, $item); $value{$name} = $value;} $num1 = $value{“num1”}; $num2 = $value{“num2”}; $sum = $num1+$num2; print "Content-type: text/html\n\n"; print “ ”; Print “ ”; Grade summary/title> print " "; Print “$sum”; Print “ ”;

31 continue Save the add.cgi Use the ftp to upload the add.cgi to server Test: open add.htm click on add

32 PERL AS A DATABASE TOOL USING DBI What is DBI( DataBase Inteface)? DBI is an interface specification for accessing databases from Perl programs Allows Perl programs to access databases in Oracle, mySQL, Informix, SQL Server, etc. DBI is a database-independent set of routines for manipulating databases  DBD (DataBase Driver)modules interface between DBI and the underlying database management system (these are specified to a particular database you intend to use)

33 Continue How do I find out which drivers (DBDs) are installed?  #!/usr/bin/perl  Use DBI;  @drivers = DBI -> available_drivers ();  Foreach $driver (@drivers) { print “ $driver\n”;}  For example, the above code may produce:  Proxy  Mysql  Oracle

34 Connecting to a database  #!/usr/bin/perl  use DBI;  $datasource = “dbi:mysql:database_name”;  $Username = “guest”;  $password = “changeme”;  $dbh = DBI - > connect ($databsource, $username, $password);  # Perform some operations on the database  $dbh ->disconnect();

35 Executing SQL Statements Use the do () routine:  $dbh -> do (“ CREAT TABLE people ( lname CHAR(10), fname CHAR(10), city CHAR(20), country CHAR(10))”);  $dbh -> do(“ INSERT INTO people VALUES (‘ALI’, ‘Setoodehnia’, ‘Sirjan’,’IRAN’)”);  $dbh -> do(INSERT INTO people VALUES (‘HONG’, ‘LI’,’Zhengzhou’,’CHINA’)”);  $dbh -> do(INSERT INTO people VALUES (‘Adel’, ‘Setoodehnia’,’Norman’,’OK’)”);


Download ppt "Perl CGI What is "CGI"? Common Gateway Interface A means of running an executable program via the Web. Perl have a *very* nice interface to create CGI."

Similar presentations


Ads by Google