Appendix F. Managing Sessions in theDatabase Tier
In Chapter 10, we discussed the development of session-based applications using the PHP session management features. In this appendix, we briefly explain how to create your own, alternative session storage functions that store session variables in a database. Specifically, this appendix covers:
Why you might want to replace the default PHP session storage layer with customized functions using the database tier
How to write user-defined PHP session storage handlers
How to create and install a fully functional set of PHP handlers that use a table in a MySQL database to store session variables
Using a Database to Keep State
The demand on popular web sites is sometimes more than can be met by a single web server. When this happens, there are two approaches you can follow: take steps to lighten the load on the server, or share the processing load across several servers. In the latter approach, each different HTTP request can be sent to a different web server, even if the requests come from the same user.
The stateless nature of HTTP permits load balancing, but PHP’s session handler undermines it. Sessions maintain variables across several HTTP requests: the variables that are written by one request are retrieved and updated by subsequent requests. By default, PHP stores session variables in files on the web server and, therefore, the web server that begins a session must process all subsequent requests that belong to that session.
If you want ...