ECLT 5830 Assignment #2 Due: 4 Mar 2018
Problem 1 (All related files are in folder “p1”) [20%]
Edit index.php so that it can output a plain text file containing the following info about
the client (in any format).
a) The operating system of the client machine as one of the following three
values: Windows, MacOS, Other (iOS is consider as “Other”)
b) The browser type as one of the following values: “Chrome”, “Microsoft Edge”,
“Firefox”, “Safari”, “Other”.
Hint: Find out which header(s) may contain hints for the above info and how to
determine the answers from the hints.
Problem 2 (All related files are in folder “p2”) [20%]
Edit only digit.php so that it can retrieve the value of a digit and the type of digit from
the request (check out index.php to figure out how these two values are embedded in
the request), and output the content of the corresponding image file. Your PHP script.
should set some header field(s) accordingly so that the browser can recognize the body
content in the response as an image.
Let # represent the value of the digit.
If “type” of digit is 1, your script. should output image "img/#CB.jpg".
If “type” of digit is 2, your script. should output image "img/#ODB.jpg"
If “type” of digit is 3, your script. should output image "img/#UNO.jpg"
For this problem, you may assume the two values are always valid.
Note: You can check if your implementation is correct by clicking the links in index.php.
If your solution is correct, you should see images of the digits in the browser (you may
want to check different browsers).
Problem 3 (All the related files are in folder “f3”) [60%]
In this problem, you need to implement a “shopping cart” and store it in PHP session
($_SESSION[]).
Characteristics of an Item
The items that you can add to the shopping cart are stored in the $mockDb[] array. You
can access this array in a PHP script. when you include “lib/items.php” in the script.
Each item has four properties:
id: A unique integer
title: A string
origin: A string
price: A float
You can study “lib/items.php” to find out how to access an item and its properties in
$mockDb[].
Files to be modified
For this problem, you need to modify three PHP scripts:
a) view_item.php
This script. should show all the properties of an item. The id of the item will be
embedded in the query string as “id=…”.
In addition, the script. should also include a form. to allow the user to specify and
submit the quantity of the item to be added to the shopping cart.
b) add_item.php
This script. needs to extract the id and the quantity of the item to be added to the
shopping cart. (You need to design how to embed these two data in the request
and write code to extract them accordingly.)
The script. should also show a message to indicate if the specified item has been
successfully added to the cart or not. In cases where item’s id is missing, is
invalid or does not identify an item in $mockDb[], your script. should output an
error message.
For simplicity, you may assume the value of the quantity is always valid.
c) view_cart.php
In this script, you need to show the content of the shopping cart. For each item in
the cart, you should at least show the following info of the item:
o ID
o Title
o Quantity
In addition, you need to design a way to allow the user to remove all items in the
cart.
Duration of a Session
For problem 3, you need to ensure each session is keep alive only for 5 minutes.
When a user revisits the website (by accessing one of the PHP scripts), if the difference
between the “current time” and the “time when the session (of the current user) was first
created” is more than five minutes, the session should be considered as “expired” and
as a result a new session should be created. When a session expires, anything saved in
the session should be considered “gone”.
To enforce a duration on a session, you will probably need to
- Set the cookie for storing the session ID an appropriate expiration time.
- Save the session creation time in the session (so that you can check on the
server side if the session has expired). [There is a chance the client can temper
with the cookie values]
- Delete an expired session and create a new one. (You can refer to the “log out”
example in the lecture note or find reference/examples on the web)
Submission
Please archive (as ZIP or ARJ) the assignment 2 folder and submit it to Blackboard.
The assignment 2 folder should contain the subfolders “p1”, “p2”, and “p3”, which in
turns contain your solution.
Anything submitted within 3 days after the deadline will receive 20% deduction.
Anything submitted within 4-7 days after the deadline will receive 50% deduction.