Page 223 - Open Soource Technologies 304.indd
P. 223
Unit 9: Web Techniques
name Notes
A unique name for a particular cookie. You can have multiple cookies with different names
and attributes.
value
The arbitrary string value attached to this cookie. The original Netscape specification
limited the total size of a cookie (including name, expiration date, and other information)
to 4 KB. So while there is no specific limit on the size of a cookie value, it probably cannot
be much larger than 3.5 KB.
expire
The expiration date for this cookie. If no expiration date is specified, the browser saves
the cookie in memory and not on disk. When the browser exits, the cookie disappears.
The expiration date is specified as the number of seconds since midnight, January 1, 1970,
GMT. For example, pass time( )+60*60*2 to expire the cookie in two hours’ time.
path
The browser will return the cookie only for URLs below this path. The default is the
directory in which the current page resides. For example, if /store/front/cart.phpsets a cookie
and does not specify a path, the cookie will be sent back to the server for all pages whose
URL path starts with /store/front/.
domain
The browser will return the cookie only for URLs within this domain. The default is the
server hostname.
secure
The browser will transmit the cookie only over https connections. The default is false,
meaning that it is okay to send the cookie over insecure connections.
When a browser sends a cookie back to the server, you can access that cookie through the
$_COOKIE array. The key is the cookie name, and the value is the cookie’s valuefield. For
instance, the following code at the top of a page keeps track of the number of times the page
has been accessed by this client:
<?php $page_accesses = $_COOKIE[‘accesses’]; setcookie(‘accesses’, ++$page_accesses); ?>
When decoding cookies, any periods (.) in a cookie’s name are turned into underscores. For
instance, a cookie named tip.top is accessible as $_COOKIE[‘tip_top’].
Example shows an HTML page that gives a range of options for background and foreground
colours.
Preference selection
<html>
<head>
<title>Set Your Preferences</title>
</head>
<body>
<form action=”prefs.php” method=”post”>
Background: <select name=”background”>
LOVELY PROFESSIONAL UNIVERSITY 217