More CGI Programming - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

More CGI Programming

Description:

User enters name or path of a file to upload. ... To get the name of the file the user wants to upload, use the param() function. ... – PowerPoint PPT presentation

Number of Views:184
Avg rating:3.0/5.0
Slides: 25
Provided by: PaulL155
Category:
Tags: cgi | more | programming | upload

less

Transcript and Presenter's Notes

Title: More CGI Programming


1
More CGI Programming
  • Here-docs
  • Multiple Submits
  • Cookies
  • Emailing
  • File Uploading

2
Things to Try if your CGI doesnt work
  • Run your script on the command line look for
    compiler errors
  • make sure proper permissions are set on file
    rwx------
  • Make sure proper permissions are set on
    directory rwxr-xr-x
  • look in error logs /servers/cgi2/logs/apache/erro
    r.log
  • make sure Windows isnt causing problems
  • dos2unix ltoldfilegt ltnewfilegt

3
Here-Doc
  • A special way of printing out large amounts of
    text
  • This can actually be done in any Perl program,
    but I find it most useful in CGI, when you have
    to print large amounts of HTML
  • print ltltHTML_END
  • lthtmlgtltheadgt
  • lttitlegttitlelt/titlegt
  • ltheadgt
  • ltbodygt
  • ...
  • HTML_END

4
Here-Doc notes
  • The ending Here-Doc marker must be on a line
    containing the marker followed by a newline.
    NOTHING ELSE.
  • Output will be formatted exactly as you type it,
    including newlines, spaces, and tabs
  • If starting marker is enclosed in double-quotes,
    or not enclosed in quotes, all variables will be
    interpolated
  • If starting marker enclosed in single-quotes,
    variables will not be interpolated
  • By convention, the Here-Doc marker is in all
    caps, much like File Handles.
  • This is convention, not a Perl specification

5
Deciding Where to Go
  • What if you want to have more than one
    functionality on your form? In other words, have
    more than one button the user can push.
  • We saw last week in the dump() example that the
    name and value of the submit button are passed as
    parameters.
  • This is useful.

6
Multiple Submits
  • Just as you can have many different text fields
    or checkboxes, you can have different submit
    buttons
  • Make sure you give each submit a different name.
  • Only the submit button that is pressed will be
    passed as a parameter.
  • Check to see if this parameter exists.
  • ltinput typesubmit nameSubmit1 valueGo Here!gt
  • ltinput typesubmit nameSubmit2 valueGo
    There!gt
  • if (param(Submit1))
  • elsif (param(Submit2))
  • else

7
Importing Variables
  • It can be tedious (and inefficient) to make
    several calls to param() to keep getting the same
    parameter value.
  • Wed like to be able to refer to those parameters
    as normal Perl variables.
  • Well, thanks to CGI.pm, we can
  • import_names() takes current parameter list and
    creates scalar and array variable for each
    parameter name, with the parameters value(s)
  • scalar is first value with that name, array is
    all
  • defaults to being imported to namespace Q
  • You can change it by passing a string into
    import_names()
  • it wont allow you to import into main

8
import_names()
  • From the inputs example last week, if outputs.cgi
    called import_names, the following variables
    would spring into existance, with the following
    values
  • QMyText ? This is a text field
  • _at_QMyText ? (This is a text field)
  • Qfruits ? Apples
  • _at_Qfruits ? (Apples, Oranges)
  • (assuming both boxes were checked)
  • Qcolors ? blue
  • _at_Qcolors ? (blue)
  • etc

9
File Uploading
  • Another input method we did not talk about last
    week is a file-upload field.
  • To use file-uploading feature, you must use a
    special kind of form.
  • Add ENCTYPEmultipart/form-data to ltformgt
  • Or, start_multipart_form() instead of
    start_form()
  • ltinput typefile nameuploadedgt
  • filefield(-namegtuploaded)
  • Creates a field in which the user can enter the
    name of the file to send to the server. Also
    creates a Browse button to search the local
    machine.
  • User enters name or path of a file to upload.
  • The form is submitted, and the CGI script can
    then access this file.

10
Getting the File
  • To get the name of the file the user wants to
    upload, use the param() function.
  • file param(uploaded)
  • If you use file as a string, it will be the name
    of the file.
  • If you use file as a filehandle, it will be a
    link to the actual file.
  • print Contents of file file areltbrgt\n
  • foreach line (ltfilegt)
  • print lineltbrgt

11
Thats Great for Text Files
  • But users can upload any kind of file.
  • You need to find out what kind of file it was.
  • uploadInfo() function. Returns a reference to a
    hash containing info about the file.
  • file param(uploaded)
  • info uploadInfo(file)
  • type info-gtContent-Type
  • type may contain text/html, text/plain,
    image/jpeg, etc etc

12
If File is not Text
  • Need a function to read from binary files.
  • read(filename, buffer, size)
  • filename?filehandle to read
  • buffer?scalar in which to store data
  • size?max number of bytes to read
  • returns number of bytes read
  • file param(uploaded)
  • open UPLOAD, gtbinary.jpg
  • while (numread(file,buf,1024))
  • print UPLOAD buf
  • close UPLOAD
  • This is not CGI-specific. read() can be used in
    any Perl-script as an alternative to ltgt

13
Emailing from your CGI Script
  • In actuality, you can use this process to email
    from any Perl program.
  • I just feel like teaching it now.
  • Note that this will be a Unix-specific (in fact,
    RPI CS dept specific) lecture. There are ways
    to accomplish the same thing on Windows, but
    were not going into it.

14
sendmail
  • barebones emailing program. No friendly user
    interface whatsoever.
  • standard with most Unix distributions.
  • on RPI CS system, located in /usr/lib/
  • but on cgi2.cs, located in /usr/sbin/
  • We need to run it with the t flag. This tells
    the program to search the message for the To,
    Cc, Bcc, etc
  • For more information, man sendmail

15
Pipes review
  • As we learned weeks ago, Pipes are one way to get
    your perl script to run an external program.
  • You can open a pipe to another program or process
    in almost the same way you open a file.
  • A pipe is a connection between your program and
    another executable program. You can feed it
    input as though you were writing to the file
  • Instead of lt, gt, or gtgt, use the character in
    front of file name.
  • open (PROG, myprogram.exe) or die Cannot
    open program

16
Put Them Together
  • open (MAIL, /usr/sbin/sendmail t) die
    Cannot begin mail program
  • print MAIL From lallip\_at_cs.rpi.edu\n
  • print MAIL To president\_at_rpi.edu\n
  • print MAIL Subject I want a raise!\n
  • print MAIL You know, Dr. J, Im not quite sure
    this is really worth it. \n
  • close MAIL

17
Cookies
  • Love them or hate them, they exist. And youll
    learn how to use them.
  • learning to use them responsibly is left as an
    exercise to the reader
  • A cookie is a (usually very small) piece of text
    that a server sends to a web browser for later
    retrieval.
  • Can be used to track a users preferences, or
    any other information the user has told the
    server.

18
To Set a Cookie
  • Create the cookie
  • cookie() function. Takes many (mostly optional)
    parameters
  • -namegt Name of the cookie
  • -valuegt Value of the cookie can be any
    scalar, including array reference or hash
    reference
  • -expiresgt Expiration date/time of the cookie
  • -pathgt Path to which cookie will be returned
  • -domaingt Domain to which cookie will be returned
  • -securegt 1 if cookie returned to SSL only

19
Cookie Expiration
  • Expires absolute or relative time for cookie to
    expire
  • 30s ? in 30 seconds
  • 10m ? in 10 minutes
  • 1h ? in one hour
  • -1d ? yesterday (ASAP)
  • now ? immediately
  • 3M ? in 3 Months
  • 10y ? in 10 Years
  • Wed, 13-Apr-2003 180000 GMT ? On Wednesday,
    4/13/2003 at 6pm GMT.

20
Cookie Path
  • region of server to check before sending back
    the cookie.
  • If I set a cookie with -path gt /perl/s03/
  • Then only CGI scripts in /perl/f03 (and its
    subdirectories) will receive the cookie.
  • By default, path is equal to the path of the
    current CGI script.
  • To send cookie to all CGI scripts on server,
    specify -path gt /

21
Cookie Domain
  • domain (or partial domain) to which to send
    cookie back
  • must contain at least 2 periods (so one cant
    send a cookie to all .com domains)
  • if I set cookies -domain gt .rpi.edu, cookie
    will be sent to scripts on www.rpi.edu,
    www.cs.rpi.edu, cgi.cs.rpi.edu, etc
  • if set to .cs.rpi.edu, cookie only sent to
    www.cs.rpi.edu, cgi.cs.rpi.edu, cgi2.cs.rpi.edu,
    etc
  • if set to www.cs.rpi.edu, cookie sent only to
    pages on www.cs.rpi.edu
  • Note that both domain and path must match cookie
    parameters in order to be sent.

22
Cookie Created, Now Set it.
  • cookie cookie( ... )
  • print header(-cookiegtcookie)
  • To set more than one cookie, use array reference
  • cookie1 cookie (...)
  • cookie2 cookie (...)
  • print header(-cookiegtcookie1, cookie2)

23
Retrieve the Cookies
  • Once again, use the cookie() function.
  • This time, dont use any value parameters. Just
    give the name
  • mycookie cookie(lallip)
  • mycookie now has value of the cookie named
    lallip.

24
Coming Weeks
  • HW5 due Tuesday, 4/8, 115959pm EDT
  • NO CLASS Wednesay, 4/9 - GMWeek
  • HW6 will be posted Wednesday, 4/9, mid-day
  • Class Wednesday 4/16 is on GUI with Perl/Tk
  • HW6 due Tuesday, 4/22, 115959pm EDT
  • Wednesday, 4/23 is a review for the Final Exam
  • Come prepared with questions
  • Well do Course Evaluations first, for those who
    dont want to stay for the review.
  • FINAL EXAM is Wednesday, 4/30
  • 400pm-600pm, in Sage 5101 and Troy 2012
Write a Comment
User Comments (0)
About PowerShow.com