You are on page 1of 19

Developing Web Applications Using PHP

Objectives

In this session, you will learn to:


Manage state using cookies
Manage state using sessions

Ver. 1.0 Slide 1 of 19


Developing Web Applications Using PHP
Managing State Using Cookies

Cookies:
Allow the website administrator to track the user’s browsing
patterns and activities on the website.
Are sent to a client computer along with the page output.
Can store data up to 4 KB.

Ver. 1.0 Slide 2 of 19


Developing Web Applications Using PHP
Identifying the Components of a Cookie

A cookie:
Is a small text file that contains information and is stored in a Web
browser.
Is sent from the server in the Set-cookie field of the HTTP header.
For example:
Set-Cookie: username=James; expires=Friday, 05-
Jun-12 22:03:38 GMT;
path=/;
Has the following components:
Name
Value
Expires
Path
Domain

Ver. 1.0 Slide 3 of 19


Developing Web Applications Using PHP
Setting a Cookie

The setcookie() function:


Is used to set the value of a cookie.
Syntax:
boolean setcookie(string $name[,string $value][,
int $expire][, string $path])
For example:
setcookie("username","Richard");

Ver. 1.0 Slide 4 of 19


Developing Web Applications Using PHP
Retrieving Cookie Information

You can retrieve the value of the cookie by passing the


name of the cookie as the key value to the $_COOKIE
associative array.
For example:
echo "Cookie Value = ".$_COOKIE["username"];
You can check whether a cookie with the specified name
exists by using the isset() function.
For example:
if (isset($_COOKIE["username"]))
echo "Hello " . $_COOKIE["username"] . "!<br />";
else
echo "User Does Not Exist !<br />";

Ver. 1.0 Slide 5 of 19


Developing Web Applications Using PHP
Removing Cookies

The setcookie() function:


Can be used to delete a cookie.
For example:
setcookie( "username", "", time() - 1);

Ver. 1.0 Slide 6 of 19


Developing Web Applications Using PHP
Just a Minute

Which global array is used for accessing the values stored


in a cookie?

Answer:
The $_COOKIE global array is used for accessing the values
stored in a cookie.

Ver. 1.0 Slide 7 of 19


Developing Web Applications Using PHP
Demo: Managing State Using Cookies

Problem Statement:
The management at Global Electronics wants its employees to
access its website for handling the customers’ requests.
However, an employee needs to sign in before accessing the
content of the website. The sign in process requires the
employee to fill a login form, which contains user-specific
information, role, and username. The role should be selected
from the provided options, such as the user and admin, before
submitting the form. After submitting the form successfully, the
Welcome page should appear that displays the message,
"Welcome <user name>".
Create a PHP application to perform this task.

Ver. 1.0 Slide 8 of 19


Developing Web Applications Using PHP
Demo: Managing State Using Cookies (Contd.)

Solution:
To create the PHP application that verifies the username and
password and redirects the user according to the role, you
need to perform the following tasks:
Create the Login form.
Create the Validate file.
Create the Home file.
Create the Admin file.
Execute the application and verify the output.

Ver. 1.0 Slide 9 of 19


Developing Web Applications Using PHP
Managing State Using Sessions

You can store any amount of information on the server by


creating a session for storing information.
Every session is identified on the server using a unique id,
known as session ID.
A session is automatically deleted when the user closes the
browser window.

Ver. 1.0 Slide 10 of 19


Developing Web Applications Using PHP
Creating a Session

session_start():
Enables you to create a session.
Must be called before the <html> tag.
For example:
<?php session_start(); ?>
<html>
<body>
</body>
</html>

Ver. 1.0 Slide 11 of 19


Developing Web Applications Using PHP
Writing Session Information

The $_SESSION global array enables you to:


Add any amount of information in a session.
For example:
$_SESSION["username"]="Mike";
Store arrays and objects in a session.
For example:
$userInfo = array( "Name" => "Mike", "Email" =>
"mike@gmail.com", "Phone" =>"98996789" );
$_SESSION["userInfo"] = $userInfo;

Ver. 1.0 Slide 12 of 19


Developing Web Applications Using PHP
Obtaining Session Information

The $_SESSION global array enables you to:


Access the session information in all the Web pages of the
website.
For example:
echo "Welcome ". $_SESSION["username"];

Ver. 1.0 Slide 13 of 19


Developing Web Applications Using PHP
Destroying a Session

session_destroy():
Enables you to destroy a session.
Syntax:
bool session_destroy()
For example:
<?php
session_destroy();
?>

Ver. 1.0 Slide 14 of 19


Developing Web Applications Using PHP
Just a Minute

Which function is used to create a session?

Answer:
The session_start() function is used to create a session.

Ver. 1.0 Slide 15 of 19


Developing Web Applications Using PHP
Demo: Managing State Using Sessions

Problem Statement:
You need to create a Web application for GlobalToyz store that
sells toys online. Topurchase toys from the company’s website,
a user needs to login. When the user is logged in successfully,
details should be stored into a session and the user should be
redirected to the page where the user name and list of available
toys are displayed. When the user selects the toy name, the toy
description and price of the toy should be displayed. In addition,
the text field should be displayed asking for the required
quantity of toys. After entering the required quantity, when the
user submits the form, all the details of the user and the toy
should get stored in a session. The stored information will be
displayed to the user with the amount of the toys to be paid.
When the user logs out, the session expires.
Create a PHP application to perform this task.

Ver. 1.0 Slide 16 of 19


Developing Web Applications Using PHP
Demo: Managing State Using Sessions (Contd.)

Solution:
To create the PHP application that validates the user name
and password entered by the user and then facilitates the user
to select the toy, you need to perform the following tasks:
Create the Login form.
Create the Validate file.
Create the Home file.
Create the Show file.
Create the Details file.
Create the Message file.
Create the Logout file.
Execute the application and verify the output.

Ver. 1.0 Slide 17 of 19


Developing Web Applications Using PHP
Summary

In this session, you learned that:


A cookie is a text file that typically contains information about
the user’s previous visits to the website, credentials, and other
information, such as the user’s desired home page, color
scheme, or other preferences.
Cookies allow the website administrator to track the user’s
browsing patterns and activities on the website.
PHP provides the built-in function, setcookie(), to set the
value of a cookie.
You can retrieve the value of the cookie by passing the name of
the cookie as the key value to the $_COOKIE associative array.
In PHP, you can store any amount of information on the server
by creating a session for storing information. Every session is
identified on the server using a unique id, known as session ID.

Ver. 1.0 Slide 18 of 19


Developing Web Applications Using PHP
Summary (Contd.)

A session is automatically deleted, when the user closes the


browser window.
You can create a session using the session_start()
function.
In PHP, you can access the session information in all the Web
pages of the website using the $_SESSION global array.

Ver. 1.0 Slide 19 of 19

You might also like