State Management in PHP
State Management in PHP
Client-Server Programming
This is Guideline PPT
It is needed to do experiments
Read detailed concept whenever
required.
By Hemant N Yadav
Agenda
• Definition of State Management
• Example of State management
• State management in PHP using Cookie
• State management in PHP using Session
• State management in PHP using URL-
Rewriting/ Query String
By Hemant N Yadav
Definition
• Management of information about client between
different http request to server known as State
management.
• Some methods are:
– Cookie: Information in small temporary files at client side
created by server and sent to server with each request.
– Session: Information in small temporary files at server side
created by server and maintained by server for each client.
– Query String/ URL Re-writing: Information about client
communicated in URL as Query string
– …… Some others are also. But we consider these three only
By Hemant N Yadav
Example of State management
• Shopping cart
– Users Login information
• Username
• Cart Information
– These information are maintained between each
http request (any click by user for next page is
called http request).
• This can be done by any of the three methods
of state management.
By Hemant N Yadav
State management in PHP using
Cookie
• In PHP
– set_cookie(“name”,”value”,timeToExpires) method
is used to store information at client temporary
file.
– $_cookie[‘cookieName’]; is a super global variable
to access cookie information in page request.
– set_cookie(“name”,”value”,timeToExpires) if
timeToExpires is 0 cookie will be deleted.
By Hemant N Yadav
State management in PHP using
Session
• In PHP
– session_start() method is used to start session(or
say to create session variable internally)
– $_SESSION[‘key’] is used to set or get data of
session specified by key.
• Ex:
– Set Session data: <?PHP $_SESSION[‘uname’] =“myName”; ?>
– Get Session Data:<?PHP echo $_SESSION[‘uname’]; ?>
– unset($_SESSION[‘uname’]) for deleting session
variable key
By Hemant N Yadav
State management in PHP using
URL Re-writing/ Query String
• In PHP
– We can access data from Query String using
• $_GET[‘QueryStringVariableName’]
– We can append information in query string While
redirecting users using header function
• header(“Location: url?QueryStringVar=Val&…”);
• Disadvantages
– Limited data
– Security of Sensitive information
By Hemant N Yadav