1 CGI (Common Gateway Interface) CmpE 587 Emir Bayraktar Onur Bük.

Slides:



Advertisements
Similar presentations
1 Copyright © 2002 Pearson Education, Inc.. 2 Chapter 1 Introduction to Perl and CGI.
Advertisements

Web forms and CGI scripts Dr. Andrew C.R. Martin
CGI & HTML forms CGI Common Gateway Interface  A web server is only a pipe between user-agents  and content – it does not generate content.
CGI Programming Part 2. Input Tags Many different ways of getting data from the user. The tag is used most often. has a type attribute –Specifies the.
Processing Form Commands in Perl for the data decoding from Web server: 1.substitute Syntax: $mystring =~ s/pattern/replacement/; This command substitutes.
JavaScript Forms Form Validation Cookies CGI Programs.
Tutorial 6 Working with Web Forms
Introduction to Web Database Processing
PZ15A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ15A - The Internet Programming Language Design and.
And so on CGI programming Web Services Java Programs for the Web.
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.
Guide To UNIX Using Linux Third Edition
Tutorial 6 Working with Web Forms. XP Objectives Explore how Web forms interact with Web servers Create form elements Create field sets and legends Create.
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.
Tutorial 6 Forms Section A - Working with Forms in JavaScript.
2440: 141 Web Site Administration Web Server-Side Programming Professor: Enoch E. Damson.
CGI Programming Languages Web Based Software Development July 21, 2005 Song, JaeHa.
 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.
CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.
UFCEKG-20-2 Data, Schemas & Applications Lecture 4 Server Side Scripting & PHP.
INTRODUCTION TO WEB DATABASE PROGRAMMING
JAVASCRIPT HOW TO PROGRAM -2 DR. JOHN P. ABRAHAM UTPA.
Chapter 9 Using Perl for CGI Programming. Computation is required to support sophisticated web applications Computation can be done by the server or the.
NETWORK CENTRIC COMPUTING (With included EMBEDDED SYSTEMS)
Comp2513 Forms and CGI Server Applications Daniel L. Silver, Ph.D.
Chapter 6: Forms JavaScript - Introductory. Previewing the Product Registration Form.
Chapter 33 CGI Technology for Dynamic Web Documents There are two alternative forms of retrieving web documents. Instead of retrieving static HTML documents,
Basics of Web Databases With the advent of Web database technology, Web pages are no longer static, but dynamic with connection to a back-end database.
2 1 Sending Data Using a Hyperlink CGI/Perl Programming By Diane Zak.
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
USING PERL FOR CGI PROGRAMMING
CSC 2720 Building Web Applications HTML Forms. Introduction  HTML forms are used to collect user input.  The collected input is typically sent to a.
Chapter 8 Cookies And Security JavaScript, Third Edition.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Using Perl for CGI Programming.
Chapter 8 Collecting Data with Forms. Chapter 8 Lessons Introduction 1.Plan and create a form 2.Edit and format a form 3.Work with form objects 4.Test.
CGI Programming. What is it? CGI –Common Gateway Interface Standard way to pass information back to the Web Server –GET Query String –POST Standard Input.
Chapter Nine Perl and CGI Programming. 2 Objectives Basic features of Perl language Set up an HTML Web page Use Perl and CGI scripts to make your web.
Chapter 9: Perl and CGI Programming CGI Programming Acknowledgement: Some materials are taken from Teach Yourself CGI Programming with PERL 5 in a Week.
Forms and Server Side Includes. What are Forms? Forms are used to get user input We’ve all used them before. For example, ever had to sign up for courses.
CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.
Approaches for creating dynamic web pages Server-side processing: Server receives a request, performs all processing necessary to create a dynamic web.
Tutorial 6 Working with Web Forms. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Explore how Web forms interact with.
3 1 Sending Data Using an Online Form CGI/Perl Programming By Diane Zak.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,
Introduction to JavaScript CS101 Introduction to Computing.
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.
CS 330 Class 9 Programming plan for today: More of how data gets into a script Via environment variables Via the url From a form By editing the url directly.
 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.
Scripting Languages Client Side and Server Side. Examples of client side/server side Examples of client-side side include: JavaScript Jquery (uses a JavaScript.
 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.
Tutorial 6 Working with Web Forms. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Explore how Web forms interact with.
Form Processing Week Four. Form Processing Concepts The principal tool used to process Web forms stored on UNIX servers is a CGI (Common Gateway Interface)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
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.
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)
1 The Internet Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
The Common Gateway Interface (CGI) Pat Morin COMP2405.
1 Chapter 1 INTRODUCTION TO WEB. 2 Objectives In this chapter, you will: Become familiar with the architecture of the World Wide Web Learn about communication.
Chapter 7 - Introduction to Common Gateway Interface (CGI)
CS 330 Class 7 Comments on Exam Programming plan for today:
DBW - PHP DBW2017.
CGI Programming Part II UNIX Security
Client side & Server side scripting
Web DB Programming: PHP
An Example of a TCP/IP Application: the World Wide Web
Presentation transcript:

1 CGI (Common Gateway Interface) CmpE 587 Emir Bayraktar Onur Bük

2 Overview What is CGI ? How it works Perl Language Cgi Environment Variables Get & POST Methods Server-Side Includes (SSI)

3 What is CGI ? It stands for Common Gateway Interface It is a way of programming for the web It is a server-side technology It makes web-pages dynamic

4 Basics of CGI CGI can be written with many languages Most popular are PERL and C Our choose is PERL because... PERL is standard on UNIX systems PERL is very easy and robust It also runs on WindowsNT

5 How it works ? CGI combines HTML tags with program statements HTML tags are for appearance and visuality PERL codes are for functionality The result are functional and nice web- pages

6 Before we begin PERL is an interpreted language You write the script and it runs But only if it is error-free A simple text editor is enough No additional software required

7 Let’s begin with the first script We need to know where PERL runs %>whereis perl or %> which perl /usr/bin/perl The first line of our script would be #!/usr/bin/perl As the result must be an HTML code print “Content-type:text/html\n\n”;

8 Default First Script : “HELLO WORLD” #!/usr/bin/perl print “Content-type:text/html\n\n”; print “ HELLO \n”; print “ \n”; print “ HELLO WORLD \n”; print “ \n”;

9 PERL Variables Like many other programming languages, PERL has different kinds of variables SCALAR VARIABLES ==> $scalar ARRAY VARIABLES HASH VARIABLES ==> %hash

10 Scalar Variables A scalar variable stores a scalar value. They have the prefix $. They can contain any kind of data like numbers, characters or a string. $num=5; $name=“John”; You can input data to a scalar variable with $name= ;

11 Array Variables An array stores list of information. They have the The arrays start with the index 0. If you refer to a single element of the array, you use the prefix $ with the index. For $city[0] =“ist” $city[2] =“izmir”

12 Hash Variables Hash variables are a special kind of arrays. They contain paired groups of elements. Each pair consists of a key and data value. They have the prefix %. %pages = ( ”Yahoo" => " ”Mail" => " ”Uni" => " );

13 CGI Environment Variables These are a series of hidden values that the web server sends to every CGI you run. They are stored in a hash called %ENV. Some of them are… DOCUMENT_ROOT:The root directory of your server HTTP_HOST:The hostname of your server REMOTE_ADDR:The IP address of the visitor REMOTE_HOST:The hostname of the visitor SERVER_NAME:Your server's qualified domain name

14 Properties of %ENV The %ENV hash is set for every CGI, and you can use any or all of it as needed. For example, you can write the IP address of the visitor with the command: print “Your IP =$ENV{‘REMOTE_ADDR'}\n"; print “Your Port =$ENV{‘REMOTE_PORT’}\n”; print “Your Browser =$ENV{‘HTTP_USER_AGENT’}\n”;

15 Some examples using %ENV Let’s find the IP address of the visitor... #!usr/bin/perl print “Content-type:text/html\n\n”; print <<Finish IP Your IP address: $ENV{‘REMOTE_ADDR’} Finish

16 GET and POST We use GET and POST in order to send data from an HTML document to a CGI program. GET: The input values are sent as part of the URL, saved in the QUERY_STRING environment variable. POST: Data are sent as input stream to the program

17 A simple example with GET We will write a simple CGI script which takes data input from and sends the data with the ‘QUERY_STRING’. The working part of the code is as follows: #!/usr/bin/perl print “Content-type:text/html\n\n” print <<Finish CMPE587

18 A simple example with GET (cont’d) This page collects info about you Name: Surname: Finish

19 A simple example with GET (cont’d)

20 There are two important results If we look at the %ENV variable we would see that $ENV{‘QUERY_STRING’}= name=Ali+Veli&sname=Yilmaz The second result is that the address bar of the result page would look like

21 How to filter unwanted characters There are some unwanted characters in the string: “=”, “+” and “&”. “+” stands for space and “&” separates input values. name=Ali+Veli&sname=Yilmaz First we have to split “name” and “sname”. Fortunately, PERL has a

22 How to filter unwanted characters (cont’d) We have now an array variable. We use the split command for the second time in order to filter “=“ character. As we have many input values we use such a code: foreach $ran { ($field, $value)= split(/=/,$ran); print “$field = $value\n”;}

23 Advantages and disadvantages of GET It is very simple to collect data and process it. You can make forms very easily. This is an advantage. It is not secure. The data that will be sent is a part of the URL. It can be easily obtained by others.

24 An example with POST POST is another method of sending data but it is much more secure than GET. Data will be sent after encoding. It can also send more data according to GET. Encoding requires decoding and this increases the complexity of the script. The decoding tasks are performed by PERL with some simple commands.

25 Decoding Commands There are two basic decoding commands. These are substitute and translate. The syntax for substitute is: $varia =~ s/pattern/replacement An example clears all: $greeting = “Hello name.\n”; $greeting = ~s/name/Ali/; print $greeting;

26 Decoding Commands The syntax for translate is: $varia =~ tr/searchlist/replacelist The translate command turns every character in the searchlist to its corresponding character in the replacelist The upper&lower case transformation can easily be done with $lower =~ tr/[A-Z]/[a-z] $upper =~ tr/[a-z]/[A-Z]

27 How to decode data streams We have seen how to split a data stream. Now, we have to decode the stream with translate and substitute. $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0- 9])/pack(“C”, hex($1))/eg;

28 How we get POST data read (STDIN, Foreach ($varname,$value)=split(/=/,$pair);, $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0- 9])/pack(“C”, hex($1))/eg; $FORM{$varname}=$value; }

29 Server-Side Includes(SSI) Embedded code section in an HTML document. Dependent on type of server Server parses SSI code executes it sends the results to the client

30 Server-Side Includes (cont’d) How does the server understand that the HTML file contains SSI code? The file extension is converted to shtml or The access rights for the file is 755 (in UNIX environment)

31 SSI Structure Ex:

32 Database Connectivity Uses Perl’s DBI Module Ex (pseudocode): use DBI; $dbh=DBI- >Connect(“dbi:mysql:dbname”,”username”,”pswd”); $sth=$dbh->prepare(“sql query”); $sth.execute; while(($var1,$var2,$var3)=$sth->fetchrow){ use variables $dbh->disconnect }

33 Thank You! Please ask any questions you may have.

34 İyi Bayramlar!