First Graded Assignment Ctec 227 Lab 2 Instructions

ctec-227-lab-2-instructions

ctec-227-lab-2-instructions

ctec-227-lab-2-instructions

ctec-227-lab-2-instructions

ctec-227-lab-2-instructions

User Manual:

Open the PDF directly: View PDF PDF.
Page Count: 2

Clark College
CTEC 227 PHP with SQL II
Cookies Page 1 of 2
Document Type: Lab No. 2
Document Name: Using Cookies
setcookie Syntax
setcookie(name,value,expire,path,domain,secure)
Parameter
Description
name
Required.
Specifies the name of the cookie
value
Required.
Specifies the value of the cookie
expire
Optional.
Specifies when the cookie expires.
time()+3600*24*30 will set the cookie to expire in 30 days. If this
parameter is not set, the cookie will expire at the end of the session
(when the browser closes).
path
Optional.
Specifies the server path of the cookie.
If set to "/", the cookie will be available within the entire domain. If
set to "/test/", the cookie will only be available within the test
directory and all sub-directories of test. The default value is the
current directory that the cookie is being set in.
domain
Optional.
Specifies the domain name of the cookie.
To make the cookie available on all subdomains of example.com
then you'd set it to ".example.com". Setting it to www.example.com
will make the cookie only available in the www subdomain
secure
Optional.
Specifies whether or not the cookie should only be transmitted over
a secure HTTPS connection. TRUE indicates that the cookie will
only be set if a secure connection exists. Default is FALSE.
In this lab exercise students will learn how to set and read cookies from PHP.
1. Create a PHP script called cookie_bake.php.
2. On this page set the following cookies as follows:
a. “username” to “BettyW” with an expiration two hours (time() + 7200)
b. “firstname” to “Bettywith an expiration of two hours (time() + 7200)
c. “lastname” to “White” with an expiration of two hours (time() + 7200)
d. Now set up 3 cookies using an array cookie:
i. setcookie("cookie[institution]","Clark", time() + 7200);
ii. setcookie("cookie[city]","Vancouver", time() + 7200);
iii. setcookie("cookie[state]","WA", time() + 7200);
Clark College
CTEC 227 PHP with SQL II
Cookies Page 2 of 2
3. Now create a PHP script called ccookie_eat.php.
4. In the this script echo the value of each cookie using $_COOKIE[]
5. For the array cookie use the following code to echo out the values of the cookies:
if (isset($_COOKIE["cookie"])){
foreach ($_COOKIE["cookie"] as $key=>$val) {
echo $key.' is '.$val."<br>\n";
} // end foreach
} // end if
6. Open the page up to set the cookies.
7. Now open up Chrome developer tools to ensure that your cookies were set correctly.
8. Create a PHP script called cookie_monster.php.
9. The cookie_monster script will be used to delete the cookies you created above.
10. To delete cookies simply set the expiration time to a time in the past. Try using time()
3600.
11. Now run your cookie_monster.php script to delete the cookies.
12. Using the Chrome developer tools check to see that your cookies were successfully
deleted.

Navigation menu