Chapter 9 PHP Cookies & Sessions

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 30

Chapter 9

Cookies & Sessions in PHP


What is Cookie?
• A cookie is a small file that the web server
stores on the client computer to be used
only by the website setting the cookies.
• Once a cookie has been set, all page
requests that follow return the cookie name
and value.
• It is often used to identify a user.
2
What is Cookie?
• A cookie created by a user can only be
visible to him. Other users cannot see its
value.

3
Why and when to use Cookies?
• Http is a stateless protocol, in other word
the server will forget everything related to
client/browser state.
• Personalizing the user experience – this is
achieved by allowing users to select their
preferences.
• Tracking the pages visited by a user.
4
Advantages of using cookies
• Cookies are simple to use and implement.
• Occupies less memory, do not require any
server resources and are stored on the
user's computer.
• Cookies persist a much longer period of
time than Session state.

5
Disadvantages of using cookies
• Cookies are not secure as they are stored in
clear text and they may pose a possible
security risk.
• Several limitations exist on the size of the
cookie text (4kb in general), number of
cookies (20 per site in general), etc.

6
Disadvantages of using cookies (cont…)
• User has the option of disabling cookies on
his computer from browser’s setting.
• Users can delete a cookies.
• Complex type of data not allowed. It allows
only plain text.

7
Creating Cookies
• A cookie is created with the setcookie ( )
function. Let’s now look at the basic syntax used
to create a cookie.
• Example:
<?php
setcookie (cookie_name, cookie_value,
[expiry_time], [cookie_path], [domain],
[secure], [httponly]);
?> 8
setcookie ( ) Parameters
Only the name and value parameters are required. All
other parameters are optional. Where,
• “setcookie” is the PHP function used to create the
cookie.
• “cookie_name” is the name of the cookie that the
server will use when retrieving its value from the
$_COOKIE array variable.
• “cookie_value” is the value of the cookie.

9
setcookie ( ) Parameters (cont…)
• “[expiry_time]” is optional; it can be used to set the
expiry time for the cookie. The time is set using the
PHP time ( ) function plus a number of seconds
greater than 0. Default is 0 which mean that the
cookie will expire at the end of the session (when the
browser closes).
• “[cookie_path]” is optional; it can be used to set the
cookie path on the server. The forward slash “/”
means that the cookie will be made available on the
entire domain. 10
setcookie ( ) Parameters (cont…)
• “[domain]” is optional, it can be used to define the
cookie access hierarchy.
• “[secure]” is optional, default is false. It is used to
determine whether the cookie is sent via https if it is
set to true or http if it is set to false.
• “[Httponly]” is optional, default is false. If it is set to
true, then only client side scripting languages
i.e. JavaScript can access them.

11
Notice
• The php setcookie function must appear before
the HTML opening tag.

12
Exmple
<?php
$cookie_name = "user";
$cookie_value = "Abdullahi Mohamed Ali";
$cookie_time = 60;
setcookie($cookie_name, $cookie_value, time
() + $cookie_time);
?>
13
Retrieving the Cookie value
• You can retrieve cookie’s values using PHP built-
in super global variable $_COOKIE (passed
cookie name as index). It contains the name and
value of the cookie.
• We also use the isset ( ) function to find out if
the cookie is set.

14
Example
<?php
if(!isset($_COOKIE[$cookie_name]))
echo ("<br>Cookie named <b>$cookie_name</
b> is not set");
else {
echo ("<br>The cookie '$cookie_name' has been set
for $cookie_time seconds");
echo ("<br>Value is: " . $_COOKIE[$cookie_name]);
}
?>
15
Delete Cookies
• If you want to destroy a cookie before its expiry
time, then you set the expiry time to a time that
has already passed.
• Example:
setcookie ($cookie_name, $cookie_value, time ( )
- 60);

16
Check if Cookies are Enabled
• Create a cookie then count the $_COOKIE array
variable.
• For example,
<?php
if(count($_COOKIE) > 0)
echo "Cookies are enabled.";
else
echo "Cookies are disabled.";
?> 17
What is a Session?
• A session is a way to store information (in global
variables) on the server to be used across
multiple pages. Unlike a cookie, the information
is not stored on the user’s computer, it’s stored
on the server.
• Each session is assigned a unique id which is
used to retrieve stored values.

18
What is a Session? (cont…)
• Sessions have the capacity to store relatively
large data compared to cookies.
• The session values are automatically deleted
when the browser is closed.
• If you want to store the values permanently,
then you should store them in the database.

19
Advantages of Sessions
• Session provide us the way of maintain user
state/data.
• Session variables are really one of the only ways of
having these variables available for the entire time
that visitor is on the website.
• It is very easy to implement.
• One big advantage of session is that we can store any
kind of data type in it.

20
Disadvantages of Sessions
• Performance overhead in case of large volumes of
data/user, because session data is stored in server
memory.

21
Retrieving Session Variables
• Like the $_COOKIE array variable, session variables
are stored in the $_SESSION array variable.
• Just like cookies, the session must be started
before any HTML tags.
• You can use sessions when developing an
application such as a shopping cart that has to
temporary store information with a capacity larger
than 4KB.
22
Creating a Session
• In order to create a session, you must first call
the PHP session_start ( ) function and then store
your values in the $_SESSION array variable.
• We use the isset ( ) function to find out if the
session is set.
• Next slide example demonstrates how to
retrieve values from sessions and displays
number of times that a page has been loaded.
23
Example
<?php
session_start ( );
if(isset($_SESSION['page_count']))
$_SESSION['page_count'] += 1;
else
$_SESSION['page_count'] = 1;
echo 'You are visitor number ' . $_SESSION['page_count'];
?>

24
Destroying Session Variables
• The session_destroy ( ) function is used to
destroy the whole PHP session variables. If you
want to destroy only a single session item, you
use the unset ( ) function.

25
Example
<?php
session_destroy ( );
unset ($_SESSION['page_count']);
?>

26
Notice
• session_destroy ( ) function removes all the
session data including cookies associated with
the session. Unset function only frees the
individual session variables. Other data remains
intact.

27
Chapter Summary
• Cookies are small files saved on the user’s
computer.
• Cookies can only be read from the issuing
domain.
• Cookies can have an expiry time, if it is not set,
then the cookie expires when the browser is
closed.

28
Chapter Summary (cont…)
• Sessions are like global variables stored on the
server.
• Each session is given a unique identification that
is used to track the variables for a user.
• Both cookies and sessions must be started
before any HTML tags have been sent to the
browser.

29
`

END
30

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy