What is CGI? The Common Gateway Interface (CGI) is a mechanism that allows Web clients to execute programs on a Web server and to receive their output.

Slides:



Advertisements
Similar presentations
Perl Practical Extration and Reporting Language An Introduction by Shwen Ho.
Advertisements

PHP I.
Adding Dynamic Content to your Web Site
Browsers and Servers CGI Processing Model ( Common Gateway Interface ) © Norman White, 2013.
JavaScript Forms Form Validation Cookies CGI Programs.
How does the server format the information it gives to the appln program? As environment variables and in standard input.
Tutorial 6 Working with Web Forms
CGI & Perl Programming Language What is CGI? Introduction of Perl Programming Language –Simple Peal program –Perl variables –Perl Operators –Flow Controls.
Guide To UNIX Using Linux Third Edition
Outcomes Know what are CGI Environment Variables Know how to use environment variables How to process A simple Query Form Able to use URL Encoding rules.
CGI Programming: Part 1. What is CGI? CGI = Common Gateway Interface Provides a standardized way for web browsers to: –Call programs on a server. –Pass.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 8: Perl Basics Fundamentals of Web Programming.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
1 ‘Dynamic’ Web Pages So far, we have developed ‘static’ web-pages, e.g., cv.html, repair.html and order.html. There is often a requirement to produce.
August Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.
Chapter 9 Using Perl for CGI Programming. Computation is required to support sophisticated web applications Computation can be done by the server or the.
1 Homework / Exam Exam 3 –Solutions Posted –Questions? HW8 due next class Final Exam –See posted schedule Websites on UNIX systems Course Evaluations.
NETWORK CENTRIC COMPUTING (With included EMBEDDED SYSTEMS)
Introduction to PHP and Server Side Technology. Slide 2 PHP History Created in 1995 PHP 5.0 is the current version It’s been around since 2004.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages.
Comp2513 Forms and CGI Server Applications Daniel L. Silver, Ph.D.
Chapter 33 CGI Technology for Dynamic Web Documents There are two alternative forms of retrieving web documents. Instead of retrieving static HTML documents,
INTERNET APPLICATION DEVELOPMENT For More visit:
JavaScript, Fourth Edition
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
Nael Alian Introduction to PHP
Python CGI programming
USING PERL FOR CGI PROGRAMMING
1 In the good old days... Years ago… the WWW was made up of (mostly) static documents. –Each URL corresponded to a single file stored on some hard disk.
Chapter 8 Cookies And Security JavaScript, Third Edition.
10/13/2015 ©2006 Scott Miller, University of Victoria 1 Content Serving Static vs. Dynamic Content Web Servers Server Flow Control Rev. 2.0.
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
ECMM6018 Enterprise Networking For Electronic Commerce Tutorial 5 Server Side Scripting Perl.
Chapter 9: Perl and CGI Programming CGI Programming Acknowledgement: Some materials are taken from Teach Yourself CGI Programming with PERL 5 in a Week.
CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.
CSC 2720 Building Web Applications Server-side Scripting with PHP.
Approaches for creating dynamic web pages Server-side processing: Server receives a request, performs all processing necessary to create a dynamic web.
Intro to PHP IST2101. Review: HTML & Tags 2IST210.
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.
Form Data Encoding GET – URL encoded POST – URL encoded
Introduction to Programming the WWW I CMSC Winter 2003.
ASP. What is ASP? ASP stands for Active Server Pages ASP is a Microsoft Technology ASP is a program that runs inside IIS IIS stands for Internet Information.
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
IT ELECTIVE 2.  Web server Can refer to either the hardware (the computer) or the software (the computer application) that helps to deliver content that.
 Previous lessons have focused on client-side scripts  Programs embedded in the page’s HTML code  Can also execute scripts on the server  Server-side.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
8 th Semester, Batch 2009 Department Of Computer Science SSUET.
 History  Ease of use  Portability  Standard  Security & Privacy  User support  Application &Popularity Today  Ten Most Popular Programming Languages.
CS 330 Class 8 Homework A pattern that contains a word with an optional period A pattern that contains Fred with a space (not Freddy) See regexp.txt guest4.htm.
Introduction to CGI PROG. CGI stands for Common Gateway Interface. CGI is a standard programming interface to Web servers that gives us a way to make.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
CGS 3066: Web Programming and Design Spring 2016 PHP.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
PHP: Further Skills 02 By Trevor Adams. Topics covered Persistence What is it? Why do we need it? Basic Persistence Hidden form fields Query strings Cookies.
1 CGI (Common Gateway Interface) CmpE 587 Emir Bayraktar Onur Bük.
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)
University of Kansas Department of Electrical Engineering and Computer Science Dr. Susan Gauch April 21, 2005 I T T C Introduction to Web Technologies.
The Common Gateway Interface (CGI) Pat Morin COMP2405.
Chapter 7 - Introduction to Common Gateway Interface (CGI)
CS 330 Class 7 Comments on Exam Programming plan for today:
PHP Introduction.
Intro to PHP & Variables
The HTTP Protocol COSC 2206 Internet Tools The HTTP Protocol
Presentation transcript:

What is CGI? The Common Gateway Interface (CGI) is a mechanism that allows Web clients to execute programs on a Web server and to receive their output. CGI applications are often used to produce HTML pages on the fly; they are also used to process the input from an HTML form. A gateway is a program that converts information to a format that a client can use; on the Web, a gateway is a program that takes non- HTML input or data and produces as output an HTML page that can be displayed by a Web browser.

How the CGI program being executed? You can execute a CGI application with your Web browser by requesting the URL that corresponds to the application. Most Web servers store CGI applications in a directory named cgi-bin. But Webmasters can define different directories for CGI applications if they so choose. One example of the URL for CGI application could be bin/MyCGIApp.cgi. bin/MyCGIApp.cgi For the information how to publish your website in LSBU server, you can visit to find more information.

How can you write a CGI application? You can write a CGI application in any language that can be executed on your Web server, compiled or interpreted. This may include the following, C, C++, FORTRAN, Perl, etc. The Perl is most popular language to write the CGI program.

CGI methods To execute a gateway program using CGI, you make a request to the server to run an application via a method. A method is a way of invoking a CGI program. In fact, methods are one of the underlying structures of HTTP; CGI methods rely on HTTP methods. The method you use defines how the program receives its data. There are three methods used for HTTP. They are: The GET method –When you use the GET method, your script receives its data in the QUERY_STRING environment variable. Your script will have to parse the QUERY_STRING environment variable to interpret the data. –The Get method should be used when the object of the request is to obtain information from the server, as opposed to changing or adding to the information on the server. The Post method –Your script receives its data from stdin (standard input) with the POST method. The server will not mark the end of the input for your script; there will be no EOF. Your script must use the CONTENT_LENGTH environment variable to determine how much data to read from standard input.

CGI Environment variables Environment variables are a series of hidden values that the web server sends to every CGI you run. Your CGI can parse them, and use the data they send. Variable Name Value DOCUMENT_ROOT The root directory of your server HTTP_COOKIE The visitor’s cookie, if one is set HTTP_HOST The hostname of your server HTTP_REFERER The URL of the page that called your script HTTP_USER_AGENT The browser type of the visitor HTTPS "on" if the script is being called through a secure server PATH The system path your server is running under QUERY_STRINGThe query string (see GET, below) REMOTE_ADDRThe IP address of the visitor

CGI Environment variables REMOTE_HOST The hostname of the visitor (if your server has reversename-lookups on; otherwise this is the IP address again) REMOTE_PORT The port the visitor is connected to on the web server REMOTE_USERThe visitor’s username (for.htaccess-protected pages) REQUEST_METHOD GET or POST REQUEST_URI The interpreted pathname of the requested document or CGI (relative to the document root) SCRIPT_FILENAME The full pathname of the current CGI SCRIPT_NAME The interpreted pathname of the current CGI (relativeto the document root) SERVER_ADMIN The address for your server’s webmaster SERVER_NAME Your server’s fully qualified domain name SERVER_PORT The port number your server is listening on SERVER_SOFTWARE The server software you’re using (such as Apache 1.3)

Basics of a Perl Script Perl language is something like HTML. It has a clearly defined syntax, and if you follow those syntax rules, you can write Perl as easily as you do HTML. Rules for the Perl programming: –Always put this line at the top of your Perl script, no excuses: #! /usr/bin/perl This line tells the server that this is a Perl script, and where to find the Perl interpreter. Make sure it is the place where Perl interpreter is, and you can always use which perl or whereis perl to find the correct place. –Always put a semicolon at the end of every line. If you don’t, you’ll never, ever, ever get a script to work

First Perl Program First perl script example: first.pl –#!/usr/bin/perl –print “Hello, world!\n”; In the Unix shell, you will need type: chmod 755 first.pl to change the file permission to allow you to run the program. And then when you type./first.pl You will get the output: Hello, world! On your screen.

Basics of a CGI Script A CGI program here is still a Perl script. But one important difference is that a CGI usually generates a web page such as form-processing CGI etc. To write a CGI that’s going to generate a HTML page, you must include this statement somewhere in the script, before you print out anything else: Print “Content-type:text/html\n\n”; This is a content header that tells the receiving web browser what sort of data it is about to receive – in this case, an HTML document.

First CGI example First CGI example first.cgi: –#!/usr/bin/perl –print "Content-type:text/html\n\n"; –print " Test Page \n"; –print " \n"; –print " Hello, world! \n"; –print " \n"; Here if you change the file permissions and run./first.cgi, you will find the script will just print out a bunch of HTML. If you use your browser call It will return a web page with the “Hello, world!” phrase on it.

First CGI example Another way to write the above CGI, without multiple print statements, is as follows: –#!/usr/bin/perl –print "Content-type:text/html\n\n"; –print <<EndOfHTML; – Test Page – – Hello, world! – –EndOfHTML This is the “here-doc” syntax, and it is very useful for you to get rid of the typing work.

Perl Variables A Scalar Variable –It stores a single (scalar) value. Perl scalar names are prefixed with a dollar sign($). So if I want to assign a value to the variable $stuff, here is how I’d do it: –A word: $stuff=”rules”; –A phrase: $stuff=”I like SBU!” –Numbers: $stuff=2 –You do not need to declare a variable before using it. A scalar can hold data of any type. You can even add and subtract like this –$stuff = 2+2 –$stuff = 4-2

An example for perl scalar variables: #!/usr/bin/perl $classname = "Apllied software for IE"; print "Hello there. What is your name?\n"; $you = ; chomp($you); print "Hello, $you. Welcome to $classname.\n"; Here the STDIN is standard input. And the chomp($you) is the function to remove the carriage return from the end of the variable $you

Perl Variables Arrays –An array variable is a variable that holds many scalar variables in numbered slots. These slots are added as needed so the variable can grow dynamically. Array variables usually have (at symbol) in front of them. Here is an example to declare an array: = (“red”,”green”,”blue”) –In Perl, just C, array indices start with 0, so $color[0] is equal to “red”. Here $ is again to refer to the single value of an array.

Perl Variables Here is another example to show how arrays work: –#!/usr/bin/perl –# this is a comment –# any line that starts with a "#" is a comment. = ("red","green","blue"); –print "$colors[0]\n"; –print "$colors[1]\n"; –print "$colors[2]\n";

Perl Variables Array Functions = ("red","green","blue","cyan","magenta", "black","yellow"); –$elt = # returns "yellow", the last value of the array. –$elt = # returns "red", the first value of the array. –You can also add data to an array: = ("green", "blue", "cyan", "magenta", "black"); # adds "orange" to the end of array now becomes ("green", "blue", "cyan", "magenta", "black", "orange"). = ("purple","teal","azure"); # appends the values to the end now becomes ("green", "blue", "cyan", "magenta", "black", "orange", "purple", "teal","azure"). RedGreem Blue Cyan magentablack yellow

Here are a few other useful functions for array manipulation: = ("green", "blue", "cyan", "magenta", "black"); # sorts the values # alphabetically now becomes ("black", "blue", "cyan", "green", "magenta" ). You can also use reverse to change the sorting order

Perl Variables Hashes –A hash is a special kind of array- an associative array, or paired group of elements. Perl hash name is prefixed with a percent sign (%), and consist of pairs of elements – a key and a data value. –Hash Name key value –%pages = ("fred", " "beth", " "john", " ); –Another way to define a hash would be as follows: –%pages = ( fred => " beth => " john => " );

Perl Variables An example for using hash: –#!/usr/bin/perl –# –# the # sign is a comment in Perl –%pages = ( "fred" => " –"beth" => " –"john" => " ); –print "Content-type:text/html\n\n"; –print <<EndHdr; – URL List – – URL List – –EndHdr

Perl Variables –foreach $key (keys %pages) { –print " $key \n";} –print <<EndFooter; – –EndFooter Foreach is a special for loop iteration

Perl Variables Output: URL List URL List john fred beth

Perl Variables Hash Functions –delete $hash{$key} # deletes the specified #key/value pair, and returns the #deleted value –exists $hash{$key} # returns true if the specified #key exists in the hash. –keys %hash # returns a list of keys for that hash –values %hash # returns a list of values for that #hash –scalar %hash # returns true if the hash has # elements defined (e.g. it’s not an #empty hash)

Perl Operators Assignment Operators 1.=Makes the value of the variable on the left side equal to whatever is on the right. 2.+=Adds the value of the right side to the left side and makes the variable on the left equal to it. 3.-=Same as above, only subtracts instead of adds.

Perl Operators Comparison Operators 1.< Returns a true value if the value on the left is less than that on the right. Otherwise false. 2.> Same as above, but the other way around. 3.>= Returns a true value if the value on the left is greater than or equal to that on the right. False if not. 4.<= Are we seeing a pattern here? 5.== Returns a true value if the values on both sides are equal; otherwise returns a false. 6.eq The same as above, but rather than comparing values, it compares strings of text. 7.!= Returns a true value if the value on the right is not equal to that on the left. False if they are equal. 8.ne Again, same as above, but for strings of text.

Perl Operators Mathematical Operators 1.* Multiplies the two values together. 2./ Divides two values. 3.+ Adds two values. 4.- Subtracts two values Adds 1 to the value on the left (so if $i were equal to 1, $i++ would equal 2) Subtracts 1 from the value on the left.

Flow Controls The For Loop –for ($i = 0; $i <= $#stuff; $i++) { print "$stuff[$i]";} The While Loop –$stuff = ; –while ($stuff ne "bob") {print "$stuff"; $stuff = ;}

Flow Controls Foreach Loop –foreach $slotnum {print "$slotnum";} –The foreach loop is really useful for running through associative arrays since their slots aren't numbered. Check this out: –foreach $slotname (keys (%stuff)) {print "$stuff{$slotname}";}