7/28/2019 Cookies Sessions
1/17
F5224
WEB PROGRAMMING
Prepared by: Munirah Abd
COOKIES
&
SESSION
7/28/2019 Cookies Sessions
2/17
COOKIES
7/28/2019 Cookies Sessions
3/17
WHAT IS COOKIES?
A cookie is often used to identify a user
To track certain user details like (No. Of Visits, names,
last visit, etc).
The client machine stores such information and
sends it to the web server whenever there is a request.
Cookies data are sent along with the HTTP headers.
7/28/2019 Cookies Sessions
4/17
HOW TO CREATE A COOKIE?
The setcookie() function is used to set a cookie.
setcookie(name, value, expire, path, domain);
SYNTAX
Note: The setcookie() function must appear BEFORE the tag.
7/28/2019 Cookies Sessions
5/17
HOW TO CREATE A COOKIE?
We will create a cookie named "user" and assign
the value "Alex Porter" to it. We also specifythat the cookie should expire after one hour:
EXAMPLE 1
.....
SYNTAX
Note: The value of the cookie is automatically URLencoded when sending the cookie,
and automatically decoded when received (to prevent URLencoding, use setrawcookie() instead).
7/28/2019 Cookies Sessions
6/17
HOW TO CREATE A COOKIE?
Set the expiration time of the cookie in another way. It
may be easier than using seconds.
EXAMPLE 2
.....
SYNTAX
Note: The expiration time is set to a month (60 sec * 60 min * 24 hours * 30 days).
7/28/2019 Cookies Sessions
7/17
HOW TO RETRIEVE A COOKIE VALUE?
The PHP $_COOKIE variable is used to retrieve a
cookie value.
SYNTAX
7/28/2019 Cookies Sessions
8/17
HOW TO RETRIEVE A COOKIE VALUE?
Use the isset() function to find out if a cookie has
been set
EXAMPLE 2
SYNTAX
7/28/2019 Cookies Sessions
9/17
HOW TO DELETE COOKIES
When deleting a cookie you should assure
that the expiration date is in the past.
SYNTAX
7/28/2019 Cookies Sessions
10/17
..QUESTION..
What if a BrowserDoes NOT Support
Cookies?
7/28/2019 Cookies Sessions
11/17
SESSIONS
7/28/2019 Cookies Sessions
12/17
WHAT IS SESSIONS?
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.
7/28/2019 Cookies Sessions
13/17
STARTING A PHP SESSION
Before you can store user information in your PHP session, you must first
start up the session.
The code below will register the user's session with the server, allow you
to start saving user information, and assign a UID for that user's session.
SYNTAX
Note: The session_start() function must appear BEFORE the tag
7/28/2019 Cookies Sessions
14/17
STORING SESSION VARIABLE
EXAMPLE
7/28/2019 Cookies Sessions
15/17
DESTROYING A SESSIONS
To delete some session data, you can use the unset() or the
session_destroy() function.
The unset() function is used to free the specified session variable
SYNTAX
7/28/2019 Cookies Sessions
16/17
DESTROYING A SESSIONS
Completely destroy the session by calling the session_destroy()
function
SYNTAX
Note: session_destroy() will reset your session and you will lose all your stored session data.
7/28/2019 Cookies Sessions
17/17
THE END