0% found this document useful (0 votes)
50 views

Unit 5

The document discusses different techniques for state and session management in web applications using PHP, including cookies, hidden fields, query strings, and server-side sessions. It explains how HTTP is stateless and these techniques allow storing and retrieving user-specific data across multiple requests. The key benefits and usage of sessions and cookies are also covered.

Uploaded by

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

Unit 5

The document discusses different techniques for state and session management in web applications using PHP, including cookies, hidden fields, query strings, and server-side sessions. It explains how HTTP is stateless and these techniques allow storing and retrieving user-specific data across multiple requests. The key benefits and usage of sessions and cookies are also covered.

Uploaded by

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

Web Development (WD) (3151606)

Unit-5

Session and State


Management using PHP

Computer Engineering Department


SVBIT, Gandhinagar
 Outline
Looping

 Need of session management


 Various techniques for state and session management like: Hidden Fields, Query String, Cookie
and Session
 HTML vs HTML5
State management System in PHP
 The basic foundation of a data communication system for the internet is HTTP (Hypertext
Transfer Protocol) which is an application layer protocol, distributive and collaborative, and
hypermedia information system.

 As we know that HTTP is a generic and stateless protocol to manage state in applications like E-
commerce, Social Media, Blog sites, many commercial sites with the help of sessions and
cookies.

 HTTP is a stateless protocol due to which it is also known to be connectionless.

 The server and client are aware of each other only when the current request after that client and
server forgot about each other, so that browser cannot get information between different request
across the web pages.
State management System in PHP
Type of state management system
1. Server-side state management system: In server-side state management system where we
used to store user specific information to identify user on server and information is available in
every web pages.

Example: session variables

2. Client side State management System: In a client-side state management system, the user
information is stored by the browser.

Example: cookies
Various techniques for state and session management
 Cookies:
 Cookies are used for client-side state management system.
 Cookies are data by the browser, cookies are sent to the web server as a piece of header
information with every HTTP request.
 Cookies can contain 1KB (1024B) size of data.

 Uses of Cookies:
 To store information about visitors regarding the accessed website’s page.
 Number of visit and views.
 Store first visit information and update it in every visit that pointed towards better experience of
user.
Various techniques for state and session management
 Type of Cookies:

 1. Session Cookie: This is a type of cookie that expires when the session will destroy.

 2. Persistent Cookie: Persistent cookie is a kind of cookie that is stored permanently on


browser’s system and expires on some specific time.

 Creation of Cookies: In PHP, we can create and set cookie by setcookie()

 Syntax:
Various techniques for state and session management
 name: It is mandatory for the time of creation, other arguments are optional.
 secure: If it is set to 1, it means it is available and sent to PHP.

 Example:

<?php
Setcookie("username", "abc", time() + 60 * 60);
?>

 Note:
 To retrieve cookies data in PHP use $_COOKIES.
 To check if it is set or not, use isset() function.
Various techniques for state and session management
 Example:

<?php
Setcookie("username", "abc", time() + 60 * 60);
?>
<html>
<body>
if(isset($_COOKIE["username"])) {
echo "Cookie is set";
}
else {
echo "Cookie is not set";
}
</body>
</html>
Various techniques for state and session management
 For updating cookies only, we need to change the argument by calling setcookie() function. We
change name “abc” to “xyz”.

<?php
setcookie("username", "xyz", time() + 60 * 60);
?>

 For deleting cookies we need to set expiry time in negative.

<?php
Setcookie("username", "xyz", time() - 3600);
?>

 Note: Drawback of using cookies is it can easily retrieve and also easily deleted. It is not secure.
Various techniques for state and session management
 Session:
 Session stores server-side information, so that all the information are accessible to all the
webpages.
 It is more secure than cookies.
 We know that HTTP is a stateless protocol so that previously performed task cannot be
remembered by current request.

 For example, when we want to buy something online, we visit many e-commerce websites and
compare products.
 Some of them are added to cart for future reference.
 After few days when we are ready to buy it, the information is still available as the cart session is
set earlier.
 Session is size independent to store as many data the user wants.
Various techniques for state and session management
 Uses of Session:
 It provides login and logout functionality to store and display relevant information.
 It maintains cart of e-commerce.
Various techniques for state and session management
 Creation of Session: In PHP, session starts from session_start() function and data can be set and
get by using global variables $_SESSION.
Various techniques for state and session management
 Retrieve information to another pages like below example:

 Output:
Various techniques for state and session management

 For updating the value of session variable


 $_SESSION["userid"]="1024";

 To destroy session in PHP, first unset all the session variable using session_unset() and
call session_destroy() methods.
Various techniques for state and session management

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