Professional Documents
Culture Documents
Sessions and Cookies Management in PHP
Sessions and Cookies Management in PHP
5
Sessions and Cookies
management in PHP
2
Attributes of a Cookie
! Name: The actual cookie text consists of the
name of the cookie and the value stored there
Creating Cookies
! A cookie is created with the PHP
built-in setcookie() function, which
takes at least one argument, the name
of the cookie.
! Syntax:
setcookie (name,value,expire );
! Note:
The setcookie() function must appear
BEFORE the <html> tag
Compiled By: Yonas H.(MSc.) Thursday, May 2, 19
5
setcookie (name ,value , expire);
! The second argument is the value that will be
stored in the cookie such as a username, date, e-
mail, and so on.
! It is not a good idea to put any kind of
sensitive personal information in cookie
files because cookie files are readable text
files.
! Other optional arguments include the expiration
date of the cookie, and the path where the cookie
is valid, and lastly, whether or not to make the
cookie secure.
Example
<?php
setcookie("user","Aster");
setcookie("color","blue");
echo $_COOKIE[“user”];
?>
Example 2
! You can also set the expiration time of
the cookie in another way.
! It may be easier than using seconds.
Deleting a Cookie
! When cookies are created, they are,
by default, deleted when the user
closes his or her browser.
! If you want to delete the cookie
right now, even before the user
closes his or her browser?You simply
subtract from the current time to
some earlier date.
Example
<?php
setcookie("cookie_name”,” ”,time( )-1);
?>
Cont. …
! In the following example we use the
isset() function to find out if a
cookie has been set:
Session
! A session is a way to store
information (in variables) to be
used across multiple pages.
! Unlike a cookie, the information is
not stored on the users computer.
! A session is the time that a user
spends at a Web site.
Session
! PHP provides us with a mechanism to
manage sessions so that we can keep
track of what a visitor is doing,
what he or she likes, what he or she
wants, and soon, even after the user
logs off.
! Like cookies, the idea is to
maintain state.
Session
! A PHP session, like a cookie, is a
way for the PHP to keep track of
that Web site visitor even after he
or she leaves or logs off.
! When you work with an application,
you open it, do some changes, and
then you close it. This is much like
a Session.
! The computer knows who you are.
! It knows when you start the application and when
you end.
Compiled By: Yonas H.(MSc.) Thursday, May 2, 19
15
Session
But on the internet there is one problem:
Session
! Session variables hold information
about one single user, and are
available to all pages in one
application.
! Tip: If you need a permanent
storage, you may want to store the
data in a database.
Session
! The session filename contains the
unique ID number for the session.
! The next time the visitor asks for
the page, his or her browser hands
the ID number back to the server.
! The server uses the session ID
number to locate the file with the
name that corresponds to the same
session ID number.
Session
! The session file contains the actual
session data;
! for example, username, preferences, or
items in the shopping cart—information
about the visitor that was stored the
last time he or she visited the page.
Session
! Sessions work by creating a unique id
(UID) for each visitor and store
variables based on this UID. The UID
is either stored in a cookie or is
propagated in the URL.
session_destroy()
! destroys all of the data associated
with the current session
Syntax: session_destroy( ) ;
Compiled By: Yonas H.(MSc.) Thursday, May 2, 19
23
Destroying a Session
! If you wish to delete some session data,you can
use the unset() or the session_destroy() function.
Cont. …
! In the example below, we create a simple page-
views counter. The isset() function checks if the
"views" variable has already been set.
output
Cont’
! To avoid that the user gets an error
message like the one above, we test
if the file exist before we try to
access it:
output
THANK YOU