CS 203 Labs 5 & 6
Due by Wednesday after each lab period
Last lab you created a form. The goal for this week is to write your own
php page, that will accept your form data, and return a
corresponding reply to the client (person who submitted the form)
and in two weeks, to
record the data from the form by appending it to a file. (append =
add to, not replace) Also
it would be a good idea to add the date and time.
Format of the "output"
What you send back to the client is the html of your php page,
with whatever else you include with php print statements. This needs to include whatever
tags might be needed, such as <br> for a new line.
Be sure to send something, the
client will be expecting it, not a total blank!
The client may try to put html tags in any text or textarea, so
these values should be "escaped" by changing, for instance, < to
< - use the php function htmlspecialchars(),
also nl2br() if you are
Note: The client may use non-ascii
(French, German, Arabic),
so you should specify the correct "charset" using a meta tag, in both
your form and action pages, or you can do the same using the php
Lab 6: Appending to a file:
For lab 6, record the data from the form by appending it to a
file. (append = add to, not
replace) Also it would be a good idea to add the date and time.
You can keep this log reasonably private by putting it in your
home directory, or available for viewing, with a link to it, by
putting it in www. (But
see abuse.html if you have a textarea)
Before your script can write to a file, you will need to
create it, and also grant write permission to All (in particular,
Others). You can do this using ssh (or putty) by giving
these commands, where obviously you replace
with the name of your choice.
- Create a file in your osiris home directory, or www
touch nameofyourfile will
create an empty file.
- Make it readable and writable by all:
For the "log" file, you have a choice of text/plain or text/html:
Writing to a file is in the course pack, here is an example, using
plain text in a www directory (can be viewed by the world):
- It could be plain text, in that case for a new line you need
to print "\n", (inside double quotes) - this is the newline
- If you would like the file to be .html, start out with some
nice HTML - styles, headings, etc. when you create it. Do
not put </body>
or </html>, leaving it "open" to grow. This doesn't overly
trouble most browsers. You can put essentially the same format
of output as you sent to the client, plus the date and time.
Perhaps end with a <hr>
You might want to use my add_html()
functions to accumulate all the output once, then print
it all and write to the file as well. To use them, include in
your php code the line:
$guest = fopen ("myguestbook.txt","a"); //open for appending
fwrite($guest, "Visit by $username\nOn");
fwrite($guest, date("d M Y - h:i a\n\n")); //eg. 07 Oct 2010 - 07:33 pm
fclose ($guest); // all done, save the file
Dealing with Abuse
Unfortunately, forms can accumulate spam. Please do one of the
- Keep your log file private. Put it in your home directory, Open it with "/home/youracct/mylogfile.txt" for example. you can't make a link to it.
- Restrict your form in some way, (no link to it, or in
/private, with .htaccess, see Private
(interactive) -- For this
assignment, youd have to give me access, so if you must, email
me how to..
- Make it public, but call my abuse function with your textarea
or other text input. You need these two lines:
abuse($comment); // die on funny stuff in $comment, for example
Completion of lab
- Make sure your form, with the new action (your php script),
and the script, are on osiris.
You fill out this form, so that I can find your form easily, and you can tell me anything you want about it, or your pages.
- I will fill out your form, and notice that it sends back my
- You have corrected the problems we reported to you from last
- I see that it also writes to a file, either
All the information I put in your form, and the date and time,
appear in that file.
- You have a link to it I can find, and you have delt wit possible abuse, see above
- It is private, I will be able to look at it since, in any
case, you will fill out the form below.
You fill out this form,
with the complete path-name of the file that you write to.
If you want to tell me more, you may use the lab 5 form above - again.
Also, if your form page has changed, please use it!
Back to Web Design