More CGI Programming - PowerPoint PPT Presentation

About This Presentation
Title:

More CGI Programming

Description:

We saw last week in the dump() example that the name and value of the submit ... must contain at least 2 periods (so can't send cookie to all .com domains) ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 22
Provided by: PaulL155
Learn more at: http://www.cs.rpi.edu
Category:
Tags: cgi | com | dump | love | message | more | programming | text

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

8
Getting the File
  • To get the name of the file user wants to upload,
    use 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

9
Thats Great for Text Files
  • But users can upload any kind of file.
  • Need to find out what kind of file it was.
  • uploadInfo() function. Returns 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

10
If File is not Text
  • Need 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

11
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.

12
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

13
Pipes
  • 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
  • For more information, CSCI-4210 CSCI-4220

14
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

15
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
    other information user has told the server.

16
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 a scalar,
    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

17
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, 17-Apr-2002 180000 GMT ? On Wednesday,
    4/17/2002 at 6pm GMT.

18
Cookie Path
  • region of server to check before sending back
    the cookie.
  • If I set a cookie with -path gt /perl/s02/
  • Then only CGI scripts in /perl/s02 (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 /

19
Cookie Domain
  • domain (or partial domain) to send cookie back
    to.
  • must contain at least 2 periods (so cant send
    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 to be set.

20
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)

21
Read 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 cookie with name
    lallip.
Write a Comment
User Comments (0)
About PowerShow.com