IT350: Web & Internet Programming

Lab 8: Cookies

Introduction

We have now learned about cookies, and more about PHP. In this lab you will practice those skills.

Lab Requirements

You must create a folder on your Web drive called "Lab08" (without the quotes) and store your work in that directory.

Download cookie_js.html from here (right-click and "Save link as...") and store it in your Lab08 directory.

1. JavaScript - Add cookies:

Modify cookie_js.html such that:

  1. Your page stores two distinct pieces of information using two cookies (e.g., user's name and favorite color)

  2. Your page uses the two pieces of information stored in cookies to display some personalized (style and/or content) content. The user should not be required to enter any information if the cookies are present.

  3. The page does something reasonable when no cookie is stored (the first time the page loads) – be sure to check this at the end!

  4. Both pieces of information can be changed by the user at any time without reloading the page, and the new information is stored in cookies. For instance, you might use text boxes or buttons to let the user reset the cookie values (e.g., change their favorite color).

  5. All of the functionality described above should be obvious to the user: what cookies are stored, how the information stored changes the page, and how the user can change what is stored.

Example: you store the user’s name and favorite color. When the page loads, the page displays their name and changes the background to their favorite color. The users can change the values stored in the cookie by clicking on buttons, causing a pop-up box to prompt them for the new information. (you do NOT have to do this specific example, but you must follow the listed requirements)

Hint: Use the functions provided in the starter code to create, read, and delete cookies.

2. PHP: Fraud Check

Next you will create a PHP script cookie_php.php that simply checks for the presence of valid cookies. The script should read one of the cookies you created with JavaScript and check that value against a list of fraudulent values stored in a file on the web server called "fraud-values.txt". Create a sample file "fraud_values.txt" that includes at least three different values (but your script should work for any number of values in the file). You pick which of the 2 distinct pieces of information stored in the cookies gets checked. Modify your cookie_php.php script so that:

  1. If a cookie is not present, the message "No cookie present" is displayed to the user in the browser
  2. If a cookie is present, and your script finds a "fraud match" (the value was found in the fraud_values.txt file), the program should display to the user "The value XXX indicates a fraud!!", where XXX is replaced by the cookie value.
  3. If a cookie is present, but your script finds no "fraud match" (the value was NOT found in the fraud_values.txt file), the program should display to the user "The value XXX does not indicate a fraud. Congratulations!" where XXX is replaced by the cookie value.
  4. Your program should work for any number of values in fraud_values.txt, not just on the sample file you created.

Test your program by fetching the URL http://mope.academy.usna.edu/~mXXXXXX/Lab08/cookie_php.php

3. default.html

  1. Add a link to Lab08/cookie_js.html to your default.html page
  2. Add a link to Lab08/cookie_php.php to your default.html page
  3. On your coversheet, explain to the instructor what cookie value is detected as being a fraud match and what cookie value is not a fraud match.

Your web page must be constructed using Notepad++ or a similar text-only editor. The use of programs such as Microsoft Word, Microsoft Frontpage, DreamWeaver, ColdFusion, Mozilla Composer, etc. will be considered an honor offense.

Deliverables

  1. Your web page with JS cookies should be called "cookie_js.html" (without the quotes).
  2. Your PHP script should be called "cookie_php.php" (without the quotes).
  3. Your pages should contain all of the elements described in the requirements section above.
  4. All of your files should be in a folder called "Lab08" (without the quotes) on the Web drive. Your instructor will assume that your web pages are viewable at http://mope.academy.usna.edu/~mXXXXXX/Lab08/cookie_js.html and http://mope.academy.usna.edu/~mXXXXXX/Lab08/cookie_php.php where XXXXXX is your alpha number. You may want to check that these URLs are viewable and that everything works correctly from a computer where somebody else is logged in. If you've goofed and linked to a file on your X drive, this will help you catch it!
  5. Turn in the following hardcopy at the beginning of class on the due date, stapled together in the following order (coversheet on top):

    1. A completed assignment coversheet. Your comments will help us improve the course.
    2. On your coversheet, give an example of cookie name and value that is detected as being a fraud match and a cookie value that is not a fraud match.
    3. A printout of the source to your cookie_php.php file (not the rendered page you see in your browser).
  6. Make sure you have the required links on your default.html page!! (see above)