CGI Programming September 19, 2001 Abdulmalik S. Al-Gahmi
Overview of Course <ul><li>Objectives Of The Course </li></ul><ul><li>Introduction to CGI </li></ul><ul><li>Preparing CGI ...
Course’s Objectives <ul><li>To understand how CGI technology works. </li></ul><ul><li>To be able to install CGI programs o...
Introduction to CGI <ul><li>CGI stands for  Common Gateway Interface . </li></ul><ul><li>CGI is a standard programming int...
Introduction to CGI (continued) <ul><li>CGI programs work as follows: </li></ul><ul><ul><li>STEP 1 (On the client side): G...
Preparing CGI for work <ul><li>The steps involved in preparing a CGI program for work. </li></ul><ul><ul><li>Contact your ...
Calling CGI Programs On The Client Side (Using HTML Forms) <ul><li><form action=“cgi-prog.cgi” method=“POST”>   . . .    <...
Calling CGI programs on the client side (Using SSI extensions) <ul><li>SSI stands for  Server Side Includes . </li></ul><u...
CGI Programming in PERL <ul><li>PERL stands for  Practical Extraction Report Language . </li></ul><ul><li>PERL  </li></ul>...
Introduction to PERL <ul><li>PERL can be downloaded from: </li></ul><ul><ul><li>http://www.perl.com/pub/a/language/info/so...
Programming Standards <ul><li>The following coding standards should be followed: </li></ul><ul><ul><li>Start the program w...
Programming Standards (Continued) <ul><li>Use three-line-comment style.  Example : </li></ul><ul><ul><li># </li></ul></ul>...
Programming Standards (Continued) <ul><li>For file handlers such as STDIN, STDOUT use all-cap identifiers. </li></ul><ul><...
PERL Data Types <ul><li>PERL has three built-in data types: scalars, arrays of scalars, and associative arrays of scalars,...
PERL Data Type (Continued) <ul><li>Array Variables </li></ul><ul><ul><li>An array is basically a way to store a whole bunc...
PERL Data Type (Continued) <ul><li>Associative Arrays “Hashes” </li></ul><ul><ul><li>Associative arrays are created with a...
Operators <ul><li>PERL uses: </li></ul><ul><ul><li>Arithmetic operations: +, -, *, /, %,**. </li></ul></ul><ul><ul><li>Rel...
Control Structures <ul><li>If /else control structure looks like: </li></ul><ul><ul><ul><li>if(condition){ </li></ul></ul>...
Control Structures (Continued) <ul><li>For loop has the following C-based formula:  </li></ul><ul><ul><li>for (initialize;...
Control Structures (Continued) <ul><ul><li>While Example : </li></ul></ul><ul><ul><li>$count=1;  </li></ul></ul><ul><ul><l...
Dealing With Files In PERL <ul><li>Reading Files: </li></ul><ul><ul><li>STEP 1: Open the file for reading: </li></ul></ul>...
Dealing With Files In PERL (Continued) <ul><li>Writing to  files: </li></ul><ul><ul><li>STEP 1: Open the file for writing:...
Dealing With Files In PERL (Continued) <ul><li>Appending Files: </li></ul><ul><ul><li>STEP 1: Open the file for appending:...
PERL Subroutines <ul><li>A subroutine is a named group of statements that does a specific job and can be called over and o...
Useful Built-in Functions <ul><li>The  chop  function is used to &quot;chop off&quot; the last character of a string varia...
Useful Built-in Functions (Continued) <ul><li>The split function separates a string expression into a list.  Example : </l...
Regular Expressions <ul><li>PERL has three primary functions that use regular expressions: split, substitute(s///) and pat...
Environment Variable <ul><li>Associative array %ENV is used to carry on the  </li></ul><ul><li>information coming from the...
Database Connection In PERL <ul><li>You can use two ways to access a database from a PERL program. </li></ul><ul><ul><li>U...
Database Connection In PERL  (Continued) <ul><li>Steps of connecting to a database. </li></ul><ul><ul><li>STEP 1: Identify...
CGI Program Examples <ul><li>PROGRAM 1: </li></ul><ul><ul><li>Suppose you have a database. You want to connect to it. What...
Upcoming SlideShare
Loading in …5
×

PowerPoint Presentation

1,861
-1

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,861
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
26
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

PowerPoint Presentation

  1. 1. CGI Programming September 19, 2001 Abdulmalik S. Al-Gahmi
  2. 2. Overview of Course <ul><li>Objectives Of The Course </li></ul><ul><li>Introduction to CGI </li></ul><ul><li>Preparing CGI Programs To Work </li></ul><ul><li>Calling CGI programs on the client side. </li></ul><ul><li>Programming CGI Using PERL </li></ul><ul><li>Connecting to and interacting with databases using ODBC. </li></ul><ul><li>CGI Program Examples </li></ul>
  3. 3. Course’s Objectives <ul><li>To understand how CGI technology works. </li></ul><ul><li>To be able to install CGI programs on the web servers. </li></ul><ul><li>To identify the ways used to connect client browsers to CGI Programs </li></ul><ul><li>To be able to write CGI programs in PERL </li></ul><ul><li>To be able to connect to a database and to run SQL commands. </li></ul>
  4. 4. Introduction to CGI <ul><li>CGI stands for Common Gateway Interface . </li></ul><ul><li>CGI is a standard programming interface to Web servers that gives us a way to make our sites dynamic and interactive. </li></ul><ul><li>CGI is not a programming language. It is just a set of standards (protocols.) </li></ul><ul><li>CGI can be implemented in an interpreted language such as PERL or in a compiled language such as C. </li></ul>
  5. 5. Introduction to CGI (continued) <ul><li>CGI programs work as follows: </li></ul><ul><ul><li>STEP 1 (On the client side): Get Information from the user (using HTML forms, SSI, Java Applet, …,etc). </li></ul></ul><ul><ul><li>STEP 2 (On the server side): Process the data, connect to DATABASE, search for PATTERNS, …,etc. </li></ul></ul><ul><ul><li>STEP 3 (On the server side): Send the result of computation back to the client. </li></ul></ul>
  6. 6. Preparing CGI for work <ul><li>The steps involved in preparing a CGI program for work. </li></ul><ul><ul><li>Contact your Web host to find out where you should put your CGI programs on the server. Usually it will be a directory on the server named “ cgi-bin ”. </li></ul></ul><ul><ul><li>Set up the access permission of directory “ cgi-bin ” using the following UNIX command: chmod 755 cgi-bin/ </li></ul></ul><ul><ul><li>Set up the access permission of the CGI program using the following UNIX command: chmod 755 cgi-program </li></ul></ul><ul><ul><li>Test your CGI program from the server command line to make sure it works perfectly before publishing it. </li></ul></ul>
  7. 7. Calling CGI Programs On The Client Side (Using HTML Forms) <ul><li><form action=“cgi-prog.cgi” method=“POST”> . . . </form> </li></ul><ul><li>Two HTTP methods are available: </li></ul><ul><ul><li>GET </li></ul></ul><ul><ul><li>POST </li></ul></ul><ul><li>The CGI program will be invoked every time you the button SUBMIT is clicked. </li></ul><ul><li>See HTMLFormDemo.html </li></ul>
  8. 8. Calling CGI programs on the client side (Using SSI extensions) <ul><li>SSI stands for Server Side Includes . </li></ul><ul><li>Two ways to invoke a cgi program here: </li></ul><ul><ul><li><!--#exec cgi=“cgi_prog.cgi&quot; --> </li></ul></ul><ul><ul><li><!--#exec cmd=“perl cgi_prog.cgi arg1 arg2 … &quot; --> </li></ul></ul><ul><li>For SSI to be used , your Web server must be already configured to accept SSI extensions. </li></ul><ul><li>Usually html files that contain SSI extensions are given the extension .shtm or .shtml. </li></ul>
  9. 9. CGI Programming in PERL <ul><li>PERL stands for Practical Extraction Report Language . </li></ul><ul><li>PERL </li></ul><ul><ul><li>Is an interpreted language. </li></ul></ul><ul><ul><li>Runs on multiple platforms:Windows, Unix, Mac, …, etc. </li></ul></ul><ul><ul><li>Is a scripting language. </li></ul></ul><ul><ul><li>Is a typeless language. </li></ul></ul><ul><ul><li>Has some aspects similar to C language. </li></ul></ul><ul><ul><li>Could be as procedural as you want it to be. </li></ul></ul><ul><ul><li>Could be as object-oriented as you want it to be (PERL 5). </li></ul></ul>
  10. 10. Introduction to PERL <ul><li>PERL can be downloaded from: </li></ul><ul><ul><li>http://www.perl.com/pub/a/language/info/software.html </li></ul></ul><ul><li>To run PERL programs: </li></ul><ul><ul><li>On UNIX, type the command from UNIX shell: </li></ul></ul><ul><ul><ul><li>perl perl_prog.pl </li></ul></ul></ul><ul><ul><li>On Windows, type the command from DOS prompt: </li></ul></ul><ul><ul><ul><li>perl perl_prog.pl </li></ul></ul></ul><ul><li>PERL is a case-sensitive language just like C or Java. </li></ul><ul><li>“ #” sign is used for comments in PERL </li></ul><ul><ul><li>Example : </li></ul></ul><ul><ul><ul><li>#!/usr/bin/perl </li></ul></ul></ul><ul><ul><ul><li># This program is to . . . </li></ul></ul></ul>
  11. 11. Programming Standards <ul><li>The following coding standards should be followed: </li></ul><ul><ul><li>Start the program with a header comment giving info about the PERL path, programmer, copyrights, last modified date, and description. </li></ul></ul><ul><ul><li>Example : </li></ul></ul><ul><ul><ul><li>#!/usr/bin/perl </li></ul></ul></ul><ul><ul><ul><li>###################################### </li></ul></ul></ul><ul><ul><ul><li># Abdulmalik S. Al-Gahmi </li></ul></ul></ul><ul><ul><ul><li># Copyrights © 2001 </li></ul></ul></ul><ul><ul><ul><li># All rights reserved. </li></ul></ul></ul><ul><ul><ul><li># </li></ul></ul></ul><ul><ul><ul><li># Last modified 9/16/2001 </li></ul></ul></ul><ul><ul><ul><li># This program is to . . . </li></ul></ul></ul><ul><ul><ul><li>###################################### </li></ul></ul></ul>
  12. 12. Programming Standards (Continued) <ul><li>Use three-line-comment style. Example : </li></ul><ul><ul><li># </li></ul></ul><ul><ul><li># Incrementing variable $count. </li></ul></ul><ul><ul><li># </li></ul></ul><ul><ul><li>$count++; </li></ul></ul><ul><li>Use meaningful names for variables or subroutines. Capitalize every word except the first one. Example : </li></ul><ul><ul><li>$myBuffer=1; </li></ul></ul><ul><ul><li>sub printThankYouMessage(){ </li></ul></ul><ul><ul><li>… } </li></ul></ul>
  13. 13. Programming Standards (Continued) <ul><li>For file handlers such as STDIN, STDOUT use all-cap identifiers. </li></ul><ul><li>Use three-space indentation. Example : </li></ul><ul><ul><li># </li></ul></ul><ul><ul><li># comment here… </li></ul></ul><ul><ul><li># </li></ul></ul><ul><ul><li>$buffer = ''; </li></ul></ul><ul><ul><li>foreach $field (@array1){ </li></ul></ul><ul><ul><li>foreach $value (@array2){ </li></ul></ul><ul><ul><li>$buffer .= &quot;$field: $value &quot;; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  14. 14. PERL Data Types <ul><li>PERL has three built-in data types: scalars, arrays of scalars, and associative arrays of scalars, known as &quot;hashes&quot;. </li></ul><ul><li>Scalar Variables </li></ul><ul><ul><li>A scalar may contain one single value in any of three different flavors: a number, a string, or a reference. </li></ul></ul><ul><ul><li>Scalar values are always named with '$‘ at the beginning, even when referring to a scalar that is part of an array or a hash. Examples : </li></ul></ul><ul><ul><li>$day #A simple scalar value &quot;day&quot; </li></ul></ul><ul><ul><li>$day[28] #the 29th element of @day </li></ul></ul><ul><ul><li>$day{'Feb'} #The 'Feb' value from %day </li></ul></ul>
  15. 15. PERL Data Type (Continued) <ul><li>Array Variables </li></ul><ul><ul><li>An array is basically a way to store a whole bunch of scalar values under one name. </li></ul></ul><ul><ul><li>An array name begins with an &quot;@&quot; symbol. Examples : </li></ul></ul><ul><ul><ul><li>@arrayName = (&quot;element1&quot;, &quot;element2&quot;); </li></ul></ul></ul><ul><ul><ul><li>@browser = (&quot;NS&quot;, &quot;IE&quot;, &quot;Opera&quot;); </li></ul></ul></ul><ul><ul><ul><li>@one_two_thre = (1, 2, 3); </li></ul></ul></ul><ul><ul><li>To access a single element is an array, use a $+array name+[ + index+]. Array indexing starts form zero. Examples : </li></ul></ul><ul><ul><ul><li>$arrayName[0] # the first element of the # array </li></ul></ul></ul><ul><ul><ul><li>$browser[1] # This will return ‘IE’. </li></ul></ul></ul>
  16. 16. PERL Data Type (Continued) <ul><li>Associative Arrays “Hashes” </li></ul><ul><ul><li>Associative arrays are created with a set of key/value pairs. A key is a text string of your choice that will help you remember the value later. </li></ul></ul><ul><ul><li>A hash name begins with % sign. Examples : </li></ul></ul><ul><ul><ul><li>%hashName = ('key1', 'value1', 'key2', 'value2'); </li></ul></ul></ul><ul><ul><ul><li>%ourFriends = ('best', 'Don', 'good', 'Robert', 'worst', 'Joe'); </li></ul></ul></ul><ul><ul><li>To access an element, use $+hash name+{+key+}. Examples : </li></ul></ul><ul><ul><ul><li>$hashName{‘key1’} #This will return value1 </li></ul></ul></ul><ul><ul><ul><li>$ourFriends{'good'} #This will return </li></ul></ul></ul><ul><ul><ul><li>#‘Robert’ </li></ul></ul></ul>
  17. 17. Operators <ul><li>PERL uses: </li></ul><ul><ul><li>Arithmetic operations: +, -, *, /, %,**. </li></ul></ul><ul><ul><li>Relational operations: <, >, <=, >=, ==. </li></ul></ul><ul><ul><li>String Operations: ., x, eq, ne, lt, gt, le, ge. </li></ul></ul><ul><ul><li>Assignment Operators: =, +=, -+, *=, /=, .=. </li></ul></ul><ul><ul><li>Increment/decrement operators: ++, --. </li></ul></ul><ul><ul><li>Boolean operations: &&, ||, !. </li></ul></ul><ul><ul><li>Quotation marks: </li></ul></ul><ul><ul><ul><li>”” character string with variable interpolation. </li></ul></ul></ul><ul><ul><ul><li>’’ character string without variable interpolation. </li></ul></ul></ul><ul><ul><ul><li>`` system commands stings with variable interpolation. </li></ul></ul></ul><ul><ul><ul><li>qq|…| character string with variable interpolation. </li></ul></ul></ul>
  18. 18. Control Structures <ul><li>If /else control structure looks like: </li></ul><ul><ul><ul><li>if(condition){ </li></ul></ul></ul><ul><ul><ul><li>If body </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>else{ </li></ul></ul></ul><ul><ul><ul><li>Else body </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><li>Example : </li></ul><ul><ul><ul><li>if ($gas_money < 10) { </li></ul></ul></ul><ul><ul><ul><li>print &quot;You don’t have enough money!&quot;; </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>else { </li></ul></ul></ul><ul><ul><ul><li>print &quot;You have enough money.&quot;; </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>
  19. 19. Control Structures (Continued) <ul><li>For loop has the following C-based formula: </li></ul><ul><ul><li>for (initialize;condition; increment){ </li></ul></ul><ul><ul><li>code to repeat </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>For Example : </li></ul></ul><ul><ul><li>for ($count=1; $count<11; $count++){ </li></ul></ul><ul><ul><li>print &quot;cool &quot;; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>While loop has also the following C-based formula: </li></ul><ul><ul><li>while (test condition) { </li></ul></ul><ul><ul><li>code to repeat </li></ul></ul><ul><ul><li>} </li></ul></ul>
  20. 20. Control Structures (Continued) <ul><ul><li>While Example : </li></ul></ul><ul><ul><li>$count=1; </li></ul></ul><ul><ul><li>while ($count < 11){ </li></ul></ul><ul><ul><li>print &quot;$count &quot;; $count++; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>foreach has the following formula: </li></ul><ul><ul><li>foreach variable_name (array_name){ </li></ul></ul><ul><ul><li>code to repeat </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>Foreach Example : </li></ul></ul><ul><ul><li>foreach $item(@inventory){ </li></ul></ul><ul><ul><li>print “$item ”; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  21. 21. Dealing With Files In PERL <ul><li>Reading Files: </li></ul><ul><ul><li>STEP 1: Open the file for reading: </li></ul></ul><ul><ul><li>open(DFH, “data.txt&quot;) || die (“Can’t Open file”); </li></ul></ul><ul><ul><li>STEP 2: Read the data from the file: </li></ul></ul><ul><ul><li>@rawData = <DFH>; </li></ul></ul><ul><ul><li>STEP 3: Close the file: </li></ul></ul><ul><ul><li>close(DFH); </li></ul></ul>
  22. 22. Dealing With Files In PERL (Continued) <ul><li>Writing to files: </li></ul><ul><ul><li>STEP 1: Open the file for writing: </li></ul></ul><ul><ul><li>open(DFH, “>data.txt&quot;) || die (“Cannot Open file”); </li></ul></ul><ul><ul><li>STEP 2: Write data to the file: </li></ul></ul><ul><ul><li>print DFH “New Data”; </li></ul></ul><ul><ul><li>STEP 3: Close the file: </li></ul></ul><ul><ul><li>close(DFH); </li></ul></ul>
  23. 23. Dealing With Files In PERL (Continued) <ul><li>Appending Files: </li></ul><ul><ul><li>STEP 1: Open the file for appending: </li></ul></ul><ul><ul><li>open(DFH, “>>data.txt&quot;) || die (“Cannot Open file”); </li></ul></ul><ul><ul><li>STEP 2: Write data at the end of the file: </li></ul></ul><ul><ul><li>print DFH “Another data line”; </li></ul></ul><ul><ul><li>STEP 3: Close the file: </li></ul></ul><ul><ul><li>close(DFH); </li></ul></ul>
  24. 24. PERL Subroutines <ul><li>A subroutine is a named group of statements that does a specific job and can be called over and over. Subroutines are very important for software reuse. </li></ul><ul><li>The formula of creating subroutines in PERL is </li></ul><ul><ul><li>sub subName(){ </li></ul></ul><ul><ul><li>group of statements </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>TO call a subroutines as follows: </li></ul><ul><ul><li>subName(arg1, arg2, arg3, …); </li></ul></ul><ul><li>PERL pass the arguments to subroutines in an array named @_ </li></ul>
  25. 25. Useful Built-in Functions <ul><li>The chop function is used to &quot;chop off&quot; the last character of a string variable </li></ul><ul><ul><li>Example : chop(“Hii”); #return “Hi” </li></ul></ul><ul><li>The length function simply gives you back the number of characters in a string variable. </li></ul><ul><ul><li>Example : length(“Hello”); #return 5 </li></ul></ul><ul><li>The substring function is a way to get a portion of a string value, rather than using the entire value. </li></ul><ul><ul><li>Example : </li></ul></ul><ul><ul><li>$str = substr(“Cold”, 1, 3); #return “old” </li></ul></ul>
  26. 26. Useful Built-in Functions (Continued) <ul><li>The split function separates a string expression into a list. Example : </li></ul><ul><ul><li>my (@pairs) = split(/&/, $buffer); </li></ul></ul><ul><li>The translate(tr) function exchanges each occurrence of a character in the search string with its matching character in the replacement string. Example : $value =~ tr/+/ /; </li></ul><ul><li>The substitute(s///) function searched the search string for the input pattern and replaces it with the replacement pattern. Example : </li></ul><ul><ul><li>$searchString =~ s/OPattern/NPattern/g; </li></ul></ul>
  27. 27. Regular Expressions <ul><li>PERL has three primary functions that use regular expressions: split, substitute(s///) and pattern-match(m//). </li></ul><ul><li>These functions are used to extract the data sent by the client before processing it on the server side. </li></ul><ul><li>Example : </li></ul><ul><ul><li>($name, $value) = split(/=/,$pair); </li></ul></ul><ul><ul><li>$value =~ tr/+/ /; </li></ul></ul><ul><ul><li>$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;,hex($1))/eg; </li></ul></ul><ul><li>See the handout titled “Regular Expressions”. </li></ul>
  28. 28. Environment Variable <ul><li>Associative array %ENV is used to carry on the </li></ul><ul><li>information coming from the client. PERL gives you </li></ul><ul><li>an access to this variable. Therefore you start your </li></ul><ul><li>CGI program by extracting this information. </li></ul>Remote user's IP address REMOTE_ADDR String in URL after the ? character QUERY_STRING Type of Browser viewer is using. HTTP_USER_AGENT Number of characters submitted from a POST command CONTENT_LENGTH What it Gets Name
  29. 29. Database Connection In PERL <ul><li>You can use two ways to access a database from a PERL program. </li></ul><ul><ul><li>Using DBI </li></ul></ul><ul><ul><li>Using ODBC </li></ul></ul><ul><li>Using ODBC is currently supported by every major software company in the world. </li></ul><ul><li>In this tutorial, MS Access will be used. Yet it will still be the same with ODBC whether you connect to MSACESS, DB2, or any other database. </li></ul>
  30. 30. Database Connection In PERL (Continued) <ul><li>Steps of connecting to a database. </li></ul><ul><ul><li>STEP 1: Identify the database drivers you have on you system. </li></ul></ul><ul><ul><li>STEP 2: Create your database created. </li></ul></ul><ul><ul><li>STEP 3: Create DSN (Date Source Name) for your database. You can do that graphically form the “CONTROL PANNEL”, or through programming . </li></ul></ul><ul><ul><li>STEP 4: Connect to the database using the Win32::ODBC module. </li></ul></ul>
  31. 31. CGI Program Examples <ul><li>PROGRAM 1: </li></ul><ul><ul><li>Suppose you have a database. You want to connect to it. What we should do is to create first a DSN. Then you need to connect to the database. Once you are connected, you can make query to the database using SQL statement. So let’s do that. </li></ul></ul><ul><li>PROGRAM 2: </li></ul><ul><ul><li>Suppose that we have a couple of listservs that we want our user to be able to subscribe or unsubscribe to. We want to create first a GUI interface (Using HTML form) that will take the the user information and send it back to the cgi program which in turn will send subscription or un-subscription to the listserv on behalf of the user. </li></ul></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×