Download presentation
Presentation is loading. Please wait.
1
CGI I: Basics Web Programming
2
CommonGatewayInterface: Intro
What is CGI? mechanism that enables programs to be run via Web Why use CGI? to create dynamic, interactive Web content How does it work? a Web browser “requests” a CGI file submits a Web form <form action=hello.cgi> clicks on a CGI link <a href=hello.cgi> loads a page with <img src=counter.cgi> CGI program is executed The output of CGI program is sent back to the browser Web Programming
3
CGI Architecture Data CGI CGI Programs Internet User (HTTP)
Web documents Web Server Web Programming
4
CGI Process: Form example
CGI program placement CGI program is placed in designated locations with appropriate file permissions User Input User submits an HTML form from a Web browser Input to CGI program Web server passes the user input to CGI program via Environment Variables: QUERY_STRING, PATH_INFO STDIN: key=value pairs Data Processing User input is processed by a CGI program to perform certain tasks Output by CGI program CGI program outputs an HTTP document to STDOUT CGI output is sent by the Web server to the browser as an HTTP message. Web Programming
5
CGI Cycle: Form Example
2. Form input 1. Submits FORM CGI Program 3. CGI output 4. HTML document User Web Server Web Programming
6
HTML Form Syntax Example
<FORM action= cgi-program method= get | post> <INPUT type=type-name name=input-name> <INPUT type=submit> </FORM> Example <form action=“cafe.cgi" method=“get”> What would you like to eat? <input type=text name=food size=15><p> What would you like to drink? <input type=text name=drink size=10><p> <input type=submit value=Order> <input type=reset value=Clear> </form> Web Programming
7
Form Data: Input to CGI Name=Value pair URL-encoded
e.g. drink=tea URL-encoded =, &, %, +, non-printable characters are converted to hex (%xx) e.g. a&b to a%26b spaces are changed to pluses name=value pairs are delimited by & e.g. name1=value1&name2=value2 Form Data: food = “steak & lobster”, drink = “beer” URL-encoded: “food=steak+%26+lobster&drink=beer” Methods for sending input to CGI POST most common method for form sent via HTTP message body, captured via STDIN GET can send small amount of information sent via URL, captured via Environment Variable QUERY_STRING URL Web Programming
8
CGI Script Decode URL-encoded form data
Perform program tasks using decoded input data Output HTTP document Syntax Line 1: Content-type HTTP header Line 2: blank Line 3+: HTML document Example Content-type: text/html <HTML> <HEAD></HEAD> <BODY>…</BODY> </HTML> Web Programming
9
CGI script: Example 1 no input, no data processing
#!/usr/bin/perl print “Content-type: text/html\n\n”; print “<HTML> <HEAD><title>Hello CGI</title></HEAD> <BODY> <h1>Hello, World!</h1> </BODY> </HTML>”; Web Programming
10
CGI.pm Perl5 CGI module Usage syntax
pre-defined functions for CGI scripts Usage syntax use CGI qw(:standard); $value = param($name); print function-name ( ); Example #!/usr/bin/perl use CGI qw(:standard); $date= param(“date”); $author= path_info(); $author= substr($author,1); print header(); print start_html(-title=>”using CGI.pm”); print h1(“Using CGI.pm”); print “This page was created using CGI.pm on $date by $author.”; print end_html(); Web Programming
11
CGI script: Example 2 Using CGI.pm and “here-document” quoting
#!/usr/bin/perl use CGI qw(:standard); $food = param("food"); $drink = param("drink"); print "Content-type: text/html\n\n"; print <<EOP; <html> <head><title>Cyber Cafe</title></head> <body> <h3>Welcome to Cyber Cafe.</h3> You ordered <b>$food</b> and <b>$drink</b>.<br> That will be \$30. </body> </html> EOP Web Programming
12
Debugging CGI Run it from the command line Run it from the browser
prog1.cgi name1=value1 name2=value2 e.g. cafe.cgi food=steak drink=beer use CGI qw(:standard –debug); # offline mode Run it from the browser check file permission, location, name use CGI::Carp qw(fatalsToBrowser); redirects error messages for server logs to browser Example Web Programming
13
CGI on WIDIT server Personal WWW root directory
URL = location = /home/stud/userid/public_html/ CGI files can be anywhere under the personal WWW root directory must have .cgi extension File Permissions CGI files has to be world executable HTML files has to be world readable All the parent directories of CGI and HTML files has to be world executable Recommended practice chmod 755 all your CGI files chmod 644 all your HTML files chmod 711 all your WWW directories (and your home directory) Web Programming
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.