Professional Documents
Culture Documents
What Is "CGI"?: - Passes Parameters To The Program
What Is "CGI"?: - Passes Parameters To The Program
1
Other Attributes of <input> Inputs that Don’t Use <input>
• <textarea name="...">...</textarea>
• name Î name of input field.
– Multi-line text field. Specify rows and cols attributes,
• value Î value returned from checks & radios; and type any pre-existing text between the tags.
text of submits and buttons; contents of text, • <select name="...">...</select>
password, and hidden
– create a drop-down menu.
• size Î width of text or password – <option value="...">...</option>
• checked Î radio or checkbox ‘turned on’ • Options in the drop down menu. value attribute is the value of
• src Î URL of an image the select input when submitted; text between tags is what is
displayed in the browser for this option
Function-Oriented
• Alternatively, you can import a set of subroutines to be
called directly, without the need to declare an object.
Outputting from CGI
• Tell Perl which subroutines to import by given a quoted list
in the use statement: • Just as CGI program took ‘input’ from user
• use CGI qw/start_html end_html header/; via web browser, it outputs back to user via
web browser as well.
• CGI.pm defines sets of these functions. Most common is
the ‘standard’ set: • STDOUT is redirected to the web browser
• use CGI qw/:standard/; that contacted it.
• For a full list of which functions are in which sets, examine • This means you don’t have to learn any new
the CGI.pm file, looking at the variable %EXPORT_TAGS output functions. print() will now
throw data to the web browser.
• Now, you can call all the CGI subroutines directly, without
declaring any objects
2
Beginning a CGI Program Now Create your Output
• #!/usr/bin/env perl • Remember, you’re building an HTML page in the
use strict; output. So you must follow the HTML format:
use warnings;
• print "<html>\n<head>\n<title>My CGI
use CGI qw/:standard/;
print header('text/html'); Program</title>\n</head>\n<body>\n";
• header() prints the HTTP header to web browser. • CGI.pm gives you a better way to do this. We’ll get to
The argument is the MIME type of the it soon.
forthcoming data.
– Defaults to text/html, so you can usually just leave the
argument out.
3
start_html() and end_html() HTML Form Shortcuts
• start_html() can take one parameter, the title. • For full list of Form shortcuts, see "FORM ELEMENTS" in
perldoc CGI
• print start_html("My title");
– or http://search.cpan.org/dist/CGI.pm/CGI.pm
– <html><head><title>My title
</title></head><body> • Each one takes parameters as name/value pairs. Name starts
with a dash. Most parameters are optional
• Can also take named parameters, with attributes to
• print startform(-method=>'post', -action=>
give to <body> tag:
'foo.cgi')
• print start_html (-title=>'My – default method is post. default action is current script
Title', -bgcolor=>'Red');
• produces: <form method="post" action="foo.cgi">
– <html><head><title>My title
</title></head><body bgcolor="Red"> • print endform(); # produces </form>
• print end_html;
– </body></html>
4
Running CGI on CS machines
• RCS does not grant CGI privileges. CGI on CS
– We'll need to use our CS Dept accounts
– If you never signed up from the course webpage, or mistakingly • The CGI-enabled server is cgi2.cs.rpi.edu
said you had a CS account when you don't, you need to email me
ASAP.
• To run your scripts on cgi2.cs
– if you never changed your temporary password, or forgot what you – set shebang: #!/usr/bin/env perl
changed your password to, email labstaff@cs.rpi.edu and request a – make file user-executable
password reset – put the file in public.html/cgi-bin directory
• The remote-access machine you should use is named – Make sure public.html and cgi-bin are world-executable
solaris.remote.cs.rpi.edu – go to http://cgi2.cs.rpi.edu/~rcsid/cgi-bin/filename.cgi
• SSH into this machine just as you have been using – see "Steps For CGI" in handouts for detailed process
rcs-sun4.rpi.edu
• If all goes well, your script’s output will be displayed.
– if they don't exist, create public.html/ and public.html/cgi-bin
directories. • If all does not go well, you’ll get HTTP 500
• Create your .cgi file in the cgi-bin directory.
Debugging CGI::Carp
• Enables you to view your compilation errors in the browser,
• Debugging a CGI program can be a very frustrating rather than searching for them in the server's error logs.
task. • #!/usr/bin/env perl
• If there are any compilation errors whatsoever, the use strict;
web browser will simply display use warnings;
500 Internal Server Error use CGI qw/:standard/;
• It will also tell you that "more information may be use CGI::Carp (
found in the server logs". 'warningsToBrowser',
'fatalsToBrowser'
• Those server logs are located on the CS System at: );
/servers/cgi2/logs/error.log print header;
• There is, of course, a nicer alternative… warningsToBrowser(1);
• fatal errors will now be displayed in the browser.
• Warnings will be displayed as HTML comments (view the
source of your page to see them)
Debugging process
• If you get HTTP 500, even with using CGI::Carp...
• Try running your script from the command line
– ssh into cgi2.cs.rpi.edu, change to your public.html/cgi-
bin directory, and run your script
• Run it *directly*: ./prog.cgi, not perl prog.cgi
• If that appears to work without problems, then check
the server logs
– try grep'ing for your username or filename:
grep lallip error.log
grep my_cgi.cgi error.log