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 data to programs on a server. – Receive responses from programs on a server. What is CGI? (cont) • CGI is the interface between server programs and other software. • CGI is not a Perl specific concept. • Any language can produce CGI programs. • Why Perl? – Perl provides a nice interface for creating CGI scripts. How Does CGI Work? • Phase 1: Create. • Phase 2: Request/Execute. • Phase 3: Respond/Display. Phase 1 • A CGI script is created. – e.g. a Perl program to do your taxes. • The script is placed on a server. – Made executable. – Given appropriate permissions. • A webpage is created and linked to the CGI script. – Webpage is the script’s interface to the world. Phase 2 • A person visits the webpage and submits a request to run the script. • Browser contacts the server with the CGI script: – Asks to run the script. – Passes input parameters to the script. • Server runs the script on the input parameters. Phase 3 • Script produces output in the form of HTML. • Server takes the output and returns it to the web browser. • Browser displays the output as an HTML page. – Page may reference other CGI scripts. An Overview of the Process Request Server HTML Input/ Output CGI Program Calling a CGI Program • CGI programs are called from HTML files. • A common/simple way is with the HREF attribute of the anchor (<a>) tag. • For example, <a href = “http://www.foo.com/cgi-bin/bar.pl”> DO IT </a> Returning the Output • CGI programs must create HTML output to return to the client. – HTML is the common language between the client and the CGI program. • Communication with client is through standard output. – Output goes to server and then goes to screen. • Use print function to send HTML output to client. HTML OUTPUT • First line of HTML output must specify the type: – i.e. type of the content of the output. • The type is usually text/html. • For example, – print “Content-type: text/html\n\n”; • There must be a blank line after the first line. A Simple Example More Powerful Interaction • Many webpages gather information from their visitors. • A more powerful interaction between CGI program and browser is needed. • This interaction is provided by forms. Forms • A form is a collection of widgets in a web page. • Solicit responses from the user. • A form must include a submit button! • When the submit button is clicked: – The CGI program specified in the form is contacted. – A string representation of the widgets’ values is sent to the server. Form Values • Remember a form’s values are sent to the server as a string. • Represented as a sequence of name=value pairs: – name is the widget’s name. – value is the widget’s value. • Values are textual only! – Other data types cannot be sent. • Validity of values must be checked by CGI program. Get vs. Post • There are two ways to send a form’s values to a CGI program. • Get is the default method. – – – – Data string is attached to URL. Seen in browser’s address bar. Server removes string from URL. Stored in environment variable, QUERY_STRING. • Post is the optional method. – Data string is sent through standard input. – CGI program can simply read it. – Length of string is stored in environment variable, CONTENT_LENGTH. Creating a Form • <form> tags are used to create a form. • For example, <form method=“post” action=“file.pl”> … <input type = “submit” value = “submit form”> </form>