Reading Assignment
-
As usual, refer to the documentation on the PHP web site and/or whatever book you are using to
learn PHP. In particular, examine the online PHP documentation for the setcookie()
function and the
$_COOKIE
“superglobal” variable.
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:
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:
-
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.
-
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.
-
What name are you going to use for your cookie?
-
What value are you going to use for your cookie?
-
Under what circumstances should the cookie not be be set?
-
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.
- Test your code using two different browsers. Try using two different colors in the two browsers. Do browsers share cookies?
- Where are cookies stored, on the server or on the user’s computer?
- 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?
- 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.
-
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:
- “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.”
- “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.