0% found this document useful (0 votes)
14 views18 pages

IP2 Chap5

The document explains the concepts of cookies and sessions in PHP, highlighting that PHP operates in a stateless environment where variables are destroyed after execution. It details how to create, retrieve, and delete cookies using the setcookie() function and the $_COOKIE variable, as well as how to manage sessions with session_start(), $_SESSION, and session_destroy(). The document provides examples for each process to illustrate the functionality of cookies and sessions in maintaining user state across web applications.

Uploaded by

Melak Enchalew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views18 pages

IP2 Chap5

The document explains the concepts of cookies and sessions in PHP, highlighting that PHP operates in a stateless environment where variables are destroyed after execution. It details how to create, retrieve, and delete cookies using the setcookie() function and the $_COOKIE variable, as well as how to manage sessions with session_start(), $_SESSION, and session_destroy(). The document provides examples for each process to illustrate the functionality of cookies and sessions in maintaining user state across web applications.

Uploaded by

Melak Enchalew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Unit Five

Cookies and Sessions


A ‘stateless’ environment
Stateless
(adj.) Having no information about what occurred previously.

Most modern applications maintain state, which


means that they remember what you were doing last
time you run the application, and they remember all
your configuration settings.
This is extremely useful because it means you can
mould the application to your working habits.
Each request for a new web page is processed without
any knowledge of previous pages requested or
processed.
Is PHP stateless?
Variables are destroyed as soon as the page script
finishes executing.
The script can access the ‘referrer’, the address of
the previous page, although this can’t really be
trusted.
$_SERVER['HTTP_REFERER']
It is possible to add data to a database/text file to
add persistent data, although this is not
connected with a particular user.
The usual way to maintain state in PHP pages is via
the use of Sessions.
PHP Cookies
Cookies are text files stored on the client computer
and they are kept of use tracking purpose. PHP
transparently supports HTTP cookies.
They can be easily viewed, modified or created by
a 3rd party.
There are three steps involved in identifying
returning users :
Server script sends a set of cookies to the
browser. For example name, age, or
identification number etc.
Browser stores this information on local
machine for future use.
When next time browser sends any request to
Cont’d…

How to Create a Cookie?


The setcookie() function is used to set a cookie.
The setcookie() function must appear before the
<html> tag.
Syntax
Cont’d…

Example

In the example below, we will create a cookie named "user"


and assign the value “Ethiopia" to it.

We also specify that the cookie should expire after one minute:
<?php
setcookie("user", "Ethiopia", time()+60);
?>
Cont’d…
You can also set the expiration time of the
cookie in another way.
It may be easier than using seconds.
<?php
$expire=time()+60*60*24*30;
setcookie("user", "Porter", $expire);
?>
This command will set the cookie called user on
the user’s PC containing the data Porter.
It will be available to all pages in the same
directory or subdirectory of the page that set it (the
default path and domain).
It will expire and be deleted after 30 days.
Cont’d…

How to Retrieve a Cookie Value?


The PHP $_COOKIE variable is used to retrieve a
cookie value.
In the example below, we retrieve the value of
the cookie named "user" and display it on a
page:
Cont’d…

<?php
// Print a cookie
echo $_COOKIE["user"];
// A way to view all cookies
print_r($_COOKIE);
?>
Cont’d…

How to Delete a Cookie?


When deleting a cookie you should assure that
the expiration date is in the past.
Example:
<?php
// set the expiration date to one hour ago
setcookie("user", "", time()-3600);
?>
PHP 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.
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.
Cont’d…

Starting a PHP Session


Before you can store user information in your PHP
session, you must first start up the session.
The session_start() function must appear before the
<html> tag:
Cont’d…

<?php
session_start();
?>
<html>
<body>
</body>
</html>
Cont’d…

Storing a Session Variable


The correct way to store and retrieve session
variables is to use the PHP $_SESSION variable:
<?php
session_start();
// store session data
$_SESSION['views']=1;
?>
Cont’d…

<html>
<body>
<?php
//retrieve session data
echo "Pageviews=". $_SESSION['views'];
?>
</body>
</html>
Cont’d…
Destroying a Session
If you wish 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:

<?php
unset($_SESSION['views']);
?>
Cont’d…

You can also completely destroy the session by


calling the session_destroy() function:
<?php
session_destroy();
?>
Note: session_destroy() will reset your session and
you will lose all your stored session data.
<?php
echo ''Thank you!'';
?>

You might also like