Cookies & Sessions
Cookies & Sessions
Cookies & Sessions
HTTP
Client server
Session
Cookie
HTTP Cookies
Cookies can also be thought of as tickets used to identify clients and their
orders
How Cookies are implemented
Cookies are sent from the server to the client via “Set-
Cookie” headers
If set to "/", the cookie will be available within the entire domain.
If set to "/phptest/", the cookie will only be available within the test directory and all
sub-directories of phptest.
The default value is the current directory that the cookie is being set in.
<?php
header(“Set-Cookie: mycookie=myvalue; path=/; domain=.coggeshall.org”);
?>
Creating cookies with setcookie()
Use the PHP setcookie() function:
Setcookie (name,value,expire, path, domain, secure)
e.g.
<?php
setcookie("MyCookie", $value, time()+3600*24);
setcookie("AnotherCookie", $value, time()+3600);
?>
<?php
?>
<?php
foreach ($_COOKIE as $key=>$val) {
print $key . " => " . $val . "<br/>";
}
?>
Warning: Cannot modify header information - headers already sent by (output started at
/var/www/html/TESTandre/159339/PHP/cookie_with_headers.php:9) in
/var/www/html/TESTandre/159339/PHP/cookie_with_headers.php on line 11
(adapted from Stobart & Parsons (2008))
Using headers
setcookie(“mycookie”);
PHP Sessions
<html>
<body>
</body>
</html>
PHP Sessions
Starting a PHP session:
<?php
session_start();
?>
session_destroy()
– Destroys all data registered to a session
– does not unset session global variables and cookies
associated with the session
–Not normally done - leave to timeout
Summary