Chapter 15
Chapter 15
Chapter 15
Managing State
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
In this chapter you will learn . . .
• Why state is a problem in web application development
• What session state is and what are its typical uses and limitations
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
The Problem of State in Web Applications
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
The Problem of State in Web Applications (ii)
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Passing Information in HTTP
In HTTP, we can pass information using:
• URL
• HTTP header
• Cookies
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Passing Information via the URL
Recall a web page can pass query
string information from the browser to
the server using one of the two
methods:
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Passing Information via HTTP Header
You can see that the form data
sent using the POST method is
sent as a query string after the
HTTP header.
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Cookies
Cookies are a client-side approach for persisting state information.
They are name=value pairs that are saved within one or more text files that
are managed by the browser.
While cookies can be used for any state-related purpose, they are principally
used as a way of maintaining continuity over time in a web application. One
typical use of cookies in a website is to “remember” the visitor so that the
server can customize the site for the user.
Cookies are also frequently used to keep track of whether a user has logged
into a site.
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
How Do Cookies Work?
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Using Cookies in PHP
$expiryTime = time()+60*60*24;
// create a persistent cookie
Cookies in PHP are $name = "username";
created using the $value = "Ricardo";
setcookie() function and setcookie($name, $value, $expiryTime);
are retrieved using the LISTING 15.2 Writing a cookie
$_COOKIES superglobal
associative array, if ( !isset($_COOKIE['username']) ) { echo "this cookie doesn't exist"; }
else {
It is important to note echo "The username retrieved from the cookie is:“. $_COOKIE['username’];
that cookies must be }
written before any other // loop through all cookies in request
page output. foreach ($_COOKIE as $name => $value) {
echo "Cookie: $name = $value";
}
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Persistent Cookie Best Practices
Due to the limitations of cookies, your site’s correct operation should not be
dependent upon them.
Almost all login systems are dependent upon IDs sent in session cookies
A login cookie might contain the username but not the password. Instead, the
login cookie would contain a random token used by the site’s back-end
database. Every time the user logs in, a new token would be generated and
stored in the database and cookie.
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Session State
Session state is a server-based
state mechanism that lets web
applications store and retrieves
objects of any type for each unique
user session.
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
How Does Session State Work?
Since HTTP is stateless, some type of
user/session identification system is
needed.
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Session Storage and Configuration
In the example shown in Figure 15.8, each user’s session information is kept
in serialized files
The decision to save sessions to files rather than in memory addresses the
issue of memory usage that can occur on shared hosts as well as persistence
between restarts.
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Session State in PHP
Session in PHP can be accessed via <?php
the $_SESSION variable, but unlike include_once("ShoppingCart.class.php");
the other superglobals, you have to session_start();
take additional steps first. // check for existence of session object before accessing
if ( !isset($_SESSION["Cart"]) ) {
You must call the session_start() $_SESSION["Cart"] = new ShoppingCart();
}
function at the beginning of the script
$cart = $_SESSION["Cart"];
?>
If the session object does not yet
exist one might generate an error, LISTING 15.8 Checking session existence
redirect to another page, or create
the required object
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Caching
As you learned back in Chapter 2, your browser uses caching to speed up the
user experience. In Chapter 10, you learned about the Web Storage API,
which provides a JavaScript-accessible cache managed by the browser.
One way to address this problem is to cache the generated markup in server
memory so that subsequent requests can be served from memory rather than
from the execution of the page.
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Page Output Caching
Page output caching saves the
rendered output of a page (or
part of a page) and reuses the
output instead of reprocessing
the page when a user requests
the page again.
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Use case for caching
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Application Data Caching
One of the biggest drawbacks of page output caching is that performance
gains will only be had if the entire cached page is the same for numerous
requests.
Then other pages that also need that same data can use the cache version
rather than retrieve it from its original location
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Redis as Caching Service
Redis is a popular in-
memory key/value noSQL
database that is frequently
used for distributed
caching.
Redis is an in-memory
database. This means its
speed of search and
retrieval is very fast.
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Key Terms
application data caching persistent cookies write-though cache
cache serialization
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Copyright
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved