1 Server Side Includes and cgi scripts Advanced Web Programming.

22
1 Server Side Includes and cgi scripts Advanced Web Programming

Transcript of 1 Server Side Includes and cgi scripts Advanced Web Programming.

Page 1: 1 Server Side Includes and cgi scripts Advanced Web Programming.

1

Server Side Includesand

cgi scripts

Advanced Web Programming

Page 2: 1 Server Side Includes and cgi scripts Advanced Web Programming.

2

Server Side IncludesSSI

• Use to add text or html to a page from another file

• Format of SSI:<!--#command parameter=“argument”-->

• Note that extension of a page that uses SSI must be .shtml on arnor.

• Note: no spaces between <!– and #• Closing bracket must be preceded by two

dashes: -->

Page 3: 1 Server Side Includes and cgi scripts Advanced Web Programming.

3

Server Side IncludesSSI

• If a page that is included itself uses SSI (to display a date in a footer for example) the extension of that page must be .shtml on icsun.

Page 4: 1 Server Side Includes and cgi scripts Advanced Web Programming.

4

SSI commands

• include include another file in this file

• echo print an environmental variable

• exec place the output of a program in an html page

Page 5: 1 Server Side Includes and cgi scripts Advanced Web Programming.

5

More SSI commands

• fsize output the size of a file

• config change the SSI error message, formatting time and file size information

• flastmod displaying the last time a file was modified.

Page 6: 1 Server Side Includes and cgi scripts Advanced Web Programming.

6

include

• Use the file parameter if the file is in the same directory

<!--#include file=“myFile.html”-->

• Use the virtual parameter if the file is in a different directory

<!--#include virtual=“/myDirect/myFile.html”-->

Page 7: 1 Server Side Includes and cgi scripts Advanced Web Programming.

7

Environmental Variables

• Variables that are maintained by the OS

• SSI can output any CGI environmental variable

• SSI can output 6 environmental variables specific to SSI

Page 8: 1 Server Side Includes and cgi scripts Advanced Web Programming.

8

SSI environmental variables

• DATE_GMT: holds date and time in GMT • DATE_LOCAL: holds date and time in local time zone • DOCUMENT_NAME: holds the current filename • DOCUMENT_URI: holds path of file from server root to

filename • LAST_MODIFIED: holds last modification date and time

for current file • QUERY_STRING_UNESCAPED: holds unaltered query

string with all shell metacharacters escaped with a "\".

Page 9: 1 Server Side Includes and cgi scripts Advanced Web Programming.

9

echo

• echo command used to output CGI environmental variables and SSI var

<!--#echo var=“LAST_MODIFIED”-->

Page 10: 1 Server Side Includes and cgi scripts Advanced Web Programming.

10

echo

• To create a back button, use echo with the HTTP_REFERER environment variable

<a href=“<!--#echo var=‘HTTP_REFERER’-->”>Back Button</a>

Page 11: 1 Server Side Includes and cgi scripts Advanced Web Programming.

11

config

• You can use config to change the appearance of time and date using the timefmt variable before calling the date.

<!--#config timefmt=“%m/%d/%y” -->

Page 12: 1 Server Side Includes and cgi scripts Advanced Web Programming.

12

config

• Other format codes see:

http://bignosebird.com/sdocs/echo.shtml

Page 13: 1 Server Side Includes and cgi scripts Advanced Web Programming.

13

exec

• Exec command used to place the output of a program in an HTML page.– The program is usually a cgi program

<!--#exec cgi=“/cgi-bin/myCGI.cgi”-->

Page 14: 1 Server Side Includes and cgi scripts Advanced Web Programming.

14

Example

• This is an example of a SSI that works on arnor:

• http://arnor.ithaca.edu/~barr/testSSI.shtmlThe code (the included file is in the same directory):

<body><!--#include file=”head.shtml"--><h1>Test 3</h1>…<p><!--#config timefmt="%d/%m/%y" --><!--#echo var="DATE_LOCAL" -->

</body></html>

Page 15: 1 Server Side Includes and cgi scripts Advanced Web Programming.

15

Mailmerge.cgi

• Mailmerge is a program that – receives input from a web form, – Formats the input according to a template– Stores the input in a file

Page 16: 1 Server Side Includes and cgi scripts Advanced Web Programming.

16

Mailmerge.cgi

• Files needed for mailmerge– The HTML form– Mailmerge.cgi– A template file– A results file

Page 17: 1 Server Side Includes and cgi scripts Advanced Web Programming.

17

Mailmerge.cgi

exForm.html

internet Web Server

Mailmerge.cgi

exSurvey.template

exSurvey.html

Info sent to web server

web server starts mailmerge and gives it info

mailmerge looks at template file to format info

mailmerge formats info and stores in results.txt

Page 18: 1 Server Side Includes and cgi scripts Advanced Web Programming.

18

MailmergeStep 1

• Procedure for storing a form on a student account on arnor– Create the html form, for example exForm.html

• In form tag, the action attribute must be

action = "/cgi-bin/mailmerge.cgi/home/xxx/public_html/exSurvey.template”

Note that this path wll be different if you put your form.html file in a directory (like ProbSet3). For every directory you go into, you must add another ../ to the path!

The same holds true here. If you put your survey.template file into a directory, you must add the directory name after the xxx in the path: cgi/home/xxx/public_html/exSurvey.template

Page 19: 1 Server Side Includes and cgi scripts Advanced Web Programming.

19

MailmergeStep 2

• Procedure for storing a form on a student account on arnor– Create the template, for example exSurvey.template

• Can be in any directory and have any name,• but the action attribute of the form tag in the exForm.html

file must point to it.

Page 20: 1 Server Side Includes and cgi scripts Advanced Web Programming.

20

MailmergeACTION=File:/~barr/exSurvey.html

REQUIRED= name,year,major

OUTPUT_FORM=Survey Results from @name@:

Year: @year@Major: @major@

Interests: @interests@

<h2> Other information</h2>

Something interesting about me: @aboutMe@

Prefered email address::@email@

**************************************************

Path to file where results will be appended

Form elements that are required

The @ surround the name of a piece of data as given in the form.html file. The name will be substituted by the value of the name.

Any other text will appear in the file exactly as shown in the template file. You can even put in html tags.

Page 21: 1 Server Side Includes and cgi scripts Advanced Web Programming.

21

MailmergeStep 3

• Procedure for storing a form on a student account on arnor– Create the exSurvey.html file

• Can be in any directory, but the template must point to it.• Must ensure that the file has write permissions

Page 22: 1 Server Side Includes and cgi scripts Advanced Web Programming.

22

Mailmerge-rw-r--r-- 1 jbarr other 3670 Aug 29 2002 form.html-rw-r--r-- 1 jbarr other 1774 Oct 9 2002 form2.html-rw-r--r-- 1 jbarr other 16752 Jan 24 2003 hp_4.jpe-rw-r--r-- 1 jbarr other 214 Sep 10 2002 lab2.c-rw-r--r-- 1 jbarr other 124 Aug 26 2002 local.cshrc-rw-r--r-- 1 jbarr other 607 Aug 26 2002 local.login-rw-r--r-- 1 jbarr other 582 Aug 26 2002 local.profile-rwxr-xr-x 1 jbarr other 21468 Aug 29 2002 mailmerge.cgi-rw-rw-rw- 1 jbarr other 2573 Sep 15 2003 exSurvey.htmldrwxr-xr-x 2 jbarr other 512 Sep 13 2002 sbin-rw-r--rw- 1 jbarr other 661 Aug 29 2002 survey.template-rw-r--r-- 1 jbarr other 261 Sep 23 2002 testIn.c-rwxr--r-- 1 jbarr other 167 Sep 3 2002 testScriptdrwxr-xr-x 2 jbarr other 512 Mar 27 09:59 theLib-rwxr-xr-x 1 jbarr other 1844 Oct 13 2002 turnIn2-rw-r--r-- 1 jbarr other 2972 Sep 18 2003 xTest.shtml

Results.txt must have write permissions:At the command line type:

chmod 666 exSurvey.htmlwhere exSurvey.html is the name of the file that you are using.