CS-90.3 Assignment 3
Due March 31

Reading Assignment

Assignment To Submit

Modify your code for Assignment 2 so that the color the user entered on the form is “pre-entered” in the form if the user returns to the page any time in the next year. You can pre-enter a value in a text input element by assigning a value to the value attribute. (“Assign a value to the value attribute?” Well, yes. The following code assigns the value “red” to the value attribute of a text input element:

<php $color = "red"; echo <<<EOT <input type="text" id="color" name="color" value="$color" /> EOT;

In your code for the index page, set $color to the value of a cookie named color if it is set; otherwise, let it be the empty string. This code should have no effect on your web page ... yet. Make sure both web pages continue to work as they did before.

The point of this assignment is for you to understand cookies, not just to get the code to work, so rather than just tell you what to do, see if you can figure it out by answering the following questions:

  1. Where do you set the cookie: in the index page or the script page?

    If we were doing JavaScript, the answer could be different. But in this case, the only possible place to set the cookie is when you know what the user typed into the form.

  2. How are you going to set the cookie before any part of the HTTP message body gets sent?

    You may well have to restructure your code from Assignment 2 to get this to work. (I had to for mine.) For example, the first part of my files looks like this:

    <php header("Content-Type application/xhtml+xml"; echo "<?xml version='1.0' encoding='utf-8'?>\n"; ?>

    The echo statement generates text that is part of the HTTP message body, so the call to setcookie() has to be executed before that echo statement is executed.

  3. What name are you going to use for your cookie?
  4. What value are you going to use for your cookie?
  5. Under what circumstances should the cookie not be be set?
  6. What if the user enters an invalid color name or hex code?

    Hint: the assignment is to pre-fill the form with whatever the user entered previously, whether it was a valid color name/hex code or not.

The hardest part of the assignment will probably be to get the cookie set before generating any page content, and without breaking the functionality of the site. The better you understand how the HTTP protocol works, the easier time you will have debugging your code.

Questions

Include answers to the following questions in the body of your email message when you submit the assignment.

  1. Test your code using two different browsers. Try using two different colors in the two browsers. Do browsers share cookies?
  2. Where are cookies stored, on the server or on the user’s computer?
  3. Given the (correct) answer to the previous question, does it make sense that you could use JavaScript, which runs in the browser on a user’s computer, to set and get cookies?
  4. If the answer to the previous question is “yes,” what information would have to be included in a JavaScript equivalent of setcookie()? If your answer to the previous question is “no,” try to come up with some explanation for why it doesn’t make sense. Try to make your explanation sound plausible even though it’s wrong.
  5. List the order of cookie-related events as a user visits your web site and submits the form the first time, and does the same thing another time less than one year later. By "cookie-related events," I mean telling what the browser sends to the server, what the PHP code on the server does, and what the server sends back to the browser.

    Your answer should start like this:

    1. “The browser sends a request message to the server for the index page; the header of the request message contains no cookie-related information because the user has never visited the site before.”
    2. “The PHP code in the index page ...”

Submission

Validate and test your code carefully before submitting it. Be sure to include your written answers to the questions in the body of your email message, along with your name.