Webmastering: Week 3
3-1 Webmastering Secrets: CGI/JAVA
This 8-week class covers state-of-the-art authoring
secrets to make an AWESOME Web page on the Internet!
Tonight is the second class in a series of eight:
Apr 18: SSI: Add modification date & templates
Apr 25: CGI: Create/process a form & image map
-> May 2: Perl: Create a guestbook and/or BBS <-
May 9: Perl: Analyze stats for who/what/when
May 16: Java: Getting it and using it
May 23: Java: Animating your page
May 30: Java: Add a search engine to your page
June 6: Java: Games and other fun stuff!
Any overview questions before we dive in?
3-2 Webmastering: TONIGHT'S PLAN
Most of you should already be familiar with basic
HTML authoring. This class will push your page to
the edge of technology and make it fun for visitors:
1) Create dynamic pages using templates & dates.
2) Let guests add content via forms to your page.
3) Use Java animations to spice up your page.
4) Master the basics of the Java language.
Tonight, we will create user interaction at your site:
1) Creating a user 'community' with return visitors
2) Create a 'starter' guestbook
3) Create a form to add a guest and their links
4) Create a shell CGI to test HTML & script
5) Have Perl modify guestbook with form info
6) Setup bulletin-board discussion areas
Any questions about what we will cover tonight?
3-3 Webmastering: HOMEWORK REVIEW
Last week, we discussed CGI scripting using Perl.
Did any of you succeed in implementing a script that
output HTML to the browser?
Any of you succeed in creating the mailus form?
Anyone have scripting problems?
Anyone care to share their Web page for others?
3-4 Webmastering: CGI USING STATIC HTML
Last week, our CGI script included the HTML code
for the form and form processing output directly
from the script. It is also possible to have CGI
modify a static HTML document on-the-fly, thereby
creating a dynamically-created static page.
You may prefer using static pages because:
1) They are probably easier to edit
2) They can be cached by Web servers and browsers
3) Do not incur overhead on EVERY page request
4) Are generally easier to debug
These static pages can be edited by Perl (or any
other script) either via a requested CGI script
or via an automated script that runs nightly.
If you MUST have a page counter, I highly suggest
using this type for 4 reasons outlined above.
Any general questions on static HTML scripting?
3-5 Webmastering: ADDING SOME INTERACTION
One of the largest complaints folks have about
most Web sites is that they lack interactivity.
The visitor is simply viewing material you create.
If you want to foster a community of users and
keep them coming back, let them contribute some
content to your site. As an added bonus, you will
get feedback you might not otherwise have gotten.
So, tonight we will create a dynamic guestbook
that visitors can add comments, URL links, and
other information to. This book could also be
extended to provide BBS-like functionality, with
different books pertaining to specific topics.
As I recommended last week and you probably found
out the hard way yourself, phased scripting is the
only way to prevent ripping your hair out debugging.
So, we will cover the guestbook in phases tonight.
Any questions before we dive into the HTML?
3-6 Webmastering: STATIC GUESTBOOK
This example and all of my HTML/code fragments
are deliberately terse. You would want to add
more useful stuff, whitespace, and blank lines
to your actual HTML/code.
First, we need a blank static guestbook to
start with:
Guestbook
The Dynamic Guestbook
Please add your comments
Note the comment noting where our script will add stuff later...any questions?
3-7 Webmastering: STATIC ADD FORM
Next, we will need a form to add entries:
Add entry
Add entry to guestbook
Fill-in the blanks below to add to guestbook:
Note that we post form results to our script that we will write next...questions?
3-8 Webmastering: TEST POINT
Now, we want to test what we have so far, so we
make a quickie shell CGI script to verify that
our previous form and CGI interface works. We
will use several of the routines in cgi-lib to
streamline our script:
First, we make sure we are in the scripts dir,
load the cgi-lib library and process CGI vars:
chdir ("d:/home/chrisw/web/scripts/");
require 'cgi-lib.pl';
&ReadParse();
Second, we use cgi-lib routines to produce a
quick document with the form variables:
print &PrintHeader;
print &HtmlTop("Form Tester");
print "
CGI/Form Variables
\n";
print &PrintVariables;
print &HtmlBot;
Now, we test everything before we go into more
detail on the Perl coding...questions?
3-9 Webmastering: GUESTBOOK SCRIPT 1 of 3
Ok, now we just need to write some Perl to take
the form results, format the fields, and place
them in the guestbook HTML at the right place:
# Make sure of the directory and load utils
chdir ('d:/home/chrisw/web/scripts');
require 'mycgilib.pl';
# Specify some files/URLs we use
$guestbookurl = "http://dqsoft.com/guestbk.shtml";
$guestbookreal = "d:/home/chrisw/web/guestbk.shtml";
$cgiurl = "http://dqsoft.com/scripts/guestbk.pl";
# Get the Date for Entry
$date = &PrintDate; #in mycgilib using localtime()
# If no CGI vars, redirect to the guestbook
if (!&ReadParse()) {
print "Location: $guestbookurl\n\n";
}
Any questions so far?
3-10 Webmastering: GUESTBOOK SCRIPT 2 of 3
Continuing the guestbook script:
# Prevent sneaky security holes...
$comments = $in{'comments'};
$comments =~ s///g; #erase SSIs
$value =~ s/<([^>]|\n)*>//g; #erase HTML
# Make sure name and comments are provided
if (!$in{'name'} || !$in{'comments'}) {
&CgiDie("Sorry, must provide name & comments");
}
# Read entire old guestbook into LINES array
open (FILE,"$guestbookreal") ||
&CgiDie("Can't open $guestbookreal");
@LINES=;
close(FILE);
$SIZE=@LINES;
# Open old guestbook for replacement
open (GUEST,">$guestbookreal") ||
&CgiDie("Can't open $guestbookreal");
Note the || (OR operand) which in Perl syntax
acts like an if statement...questions so far?
3-11 Webmastering: GUESTBOOK SCRIPT 3 of 3
Continuing the guestbook script:
# Copy old guestbook contents except at entry point
for ($i=0;$i<=$SIZE;$i++) {
$_=$LINES[$i];
if (!(//)) {
print GUEST $_;
} else {
# Insert our new entry here
print GUEST "\n";
print GUEST "$comments
\n";
print GUEST "By: $in{'name'}";
if ($in{'url'}) {
print GUEST "
Recommends ";
print GUEST "$in{url}\n";
} # repeat this for email (I skipped)
print GUEST " on $date\n
\n";
}
} #for loop
# Close new guestbook and redirect user to it
close (GUEST);
print "Location: $guestbookurl\n\n";
Note the // search pattern used, allows
us to easily find the right spot...questions?
3-12 Webmastering: CREATING BBS TOPICS
This basic guestbook can be modified to do many
other useful things. You could create several
BBS boards on topics, similar to what we have
here on INN. To do this:
1) Create empty HTML boards for each topic.
2) Create a single addform with a drop-down
for topic, or use separate addforms.
3) Use a single add script with if statements
that choose the correct topic file based
on the topic field.
There are LOTS of ways to utilize these same
ideas, so be creative:
1) Write an automated script that changes
your page's cover graphic every evening.
2) Create a form that would allow you to auto-
matically add new links to your link pages.
3) Allow a last-visitor comment for all to see.
3-13 Webmastering: CONCLUSION
This concludes tonight's class.
Next week's class will start promptly at the same time.
You can mail me here at box 952 or via chrisw@dqsoft.com
for questions or help with your pages. You will find
my page w/links and samples at http://dqsoft.com/
Next week, we will analyze the visitors at your site:
1) Finding your servers log files
2) What information is available to analyze
3) Pulling your site-specifics out of the mass
4) How to use Perl to compile statistics
5) Presenting these in HTML with tables and graphs
6) Running this script automatically every night
HOMEWORK: Implement a guestbook or some other static
HTML document and a script that edits it.
Now we have an open question and answer session where
you can get me to debug your scripts for you...