4/6/2010
Web Engineering by Hassan Khan
A cookie is often used to identify a user.
A cookie is a small file that the server embeds
on the user's computer.
Each time the same computer requests a
page with a browser, it will send the cookie
too.
Web Engineering by Hassan Khan
1
4/6/2010
setcookie(name, value, expire, path,
domain);
setcookie("user", “Hassan", time()+3600);
$expire=time()+60*60*24*30;
setcookie("user", “Hassan", $expire);
Web Engineering by Hassan Khan
The PHP $_COOKIE variable is used to retrieve a cookie value.
// Print a cookie
echo $_COOKIE["user"];
// A way to view all cookies
print_r($_COOKIE);
Web Engineering by Hassan Khan
2
4/6/2010
Web Engineering by Hassan Khan
A PHP session variable is used to store
information about, or change settings for a
user session.
Session variables hold information about one
single user, and are available to all pages in
one application.
However, session information is temporary
and will be deleted after the user has left the
website.
Web Engineering by Hassan Khan
3
4/6/2010
The session_start() function must appear
BEFORE the <html> tag:
Web Engineering by Hassan Khan
The correct way to store and retrieve session
variables is to use the PHP $_SESSION
variable.
Web Engineering by Hassan Khan
4
4/6/2010
Web Engineering by Hassan Khan
The isset() function checks if the "views"
variable has already been set.
Web Engineering by Hassan Khan
5
4/6/2010
If "views" has been set, we can increment our
counter. If "views" doesn't exist, we create a
"views" variable, and set it to 1
Web Engineering by Hassan Khan
unset()
The unset() function is used to free the
specified session variable.
session_destroy()
You can also completely destroy the session by
calling the session_destroy() function
Web Engineering by Hassan Khan