8.2. Session Management Over the Web
Storing the state in the web server—the middle tier—can solve the problem of increased request size and protect the state of an application from accidental or intentional changes a user might make.
A session is a way to identify and manage the state—the session variables -- for a particular user. When a user sends an HTTP request, the middle tier must process the current request in the context of the user's session. When a session is started, the client is given a session identifier -- often a cookie—that is included with subsequent requests to the server. The server uses the session identifier to locate the corresponding session before processing the request.
Rather than storing all the variables needed to maintain state and include them with each request, the browser stores a single session identifier that finds and initializes the variables stored on the server. The session identifier is like the ticket given at a cloak room. The ticket is much easier to carry around and ensures that the holder gets her own hat and coat.
One implication of storing session variables in the middle tier is that data needs to be stored for each session. The question is, for how long? Because HTTP is stateless, there is no way to know when a user has finished with a session. Ideally, the user logs out of an application, and the logout script ends the session. However, because a server can never be sure if a user is still there, the server needs to clean up old sessions that have not been used for a period of time. This last point is important, because sessions consume resources on the server, and dormant sessions may present a security risk. How long the timeout should be depends on the needs of the application, and we discuss this in more detail later in this chapter.
In summary, there are three characteristics session management over the Web must exhibit:
Copyright © 2003 O'Reilly & Associates. All rights reserved.