CS 203 Labs 5 & 6

Due by Wednesday after each lab period

The parts about writing to a file are to be added in Lab 6.
Requrements and submission checklists

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 &lt; - use the php function htmlspecialchars(), also nl2br() if you are expecting poetry.

Note: The client may use non-ascii characters (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 header() function.

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 nameofyourfile with the name of your choice.

  1. Create a file in your osiris home directory, or www    touch nameofyourfile   will create an empty file.
  2. Make it readable and writable by all:  chmod a+rw nameofyourfile

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):
$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 following:
  1. 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.
  2. 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..
  3. 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

Lab 5

  1. Make sure your form, with the new action (your php script), and the script, are on osiris.
  2. 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.
    Your Linux code
    Name (only) of the page where your form is: (NOT the form's action)
    What you want to tell me about your form or what you did new this week:

  3. I will fill out your form, and notice that it sends back my information.

Lab 6

  1. You have corrected the problems we reported to you from last week.
  2. I see that it also writes to a file, either
  3. All the information I put in your form, and the date and time, appear in that file.
  4. You fill out this form, with the complete path-name of the file that you write to. Example: /home/astudent/mylog.txt
Your linux username:
Path to your log file:
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