11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

www.it-ebooks.infoC H A P T E R 18• • •Session HandlersAlthough available since the version 4.0 release, <strong>PHP</strong>’s session-handling capabilities remain one of thecoolest and most discussed features. In this chapter, you’ll learn the following:• Why session handling is necessary, and useful• How to configure <strong>PHP</strong> to most effectively use the feature• How to create and destroy sessions, and manage session variables• Why you might consider managing session data in a database, and how to do itWhat Is Session Handling?The Hypertext Transfer Protocol (HTTP) defines the rules used to transfer text, graphics, video, and allother data via the World Wide Web. It is a stateless protocol, meaning that each request is processedwithout any knowledge of any prior or future requests. Although HTTP’s simplicity is a significantcontributor to its ubiquity, its stateless nature has long been a problem for developers who wish tocreate complex Web-based applications that must adjust to user-specific behavior and preferences. Toremedy this problem, the practice of storing bits of information on the client’s machine, in what arecommonly called cookies, quickly gained acceptance, offering some relief to this conundrum. However,limitations on cookie size, the number of cookies allowed, and various other inconveniencessurrounding their implementation prompted developers to devise another solution: session handling.Session handling is essentially a clever workaround to this problem of statelessness. This isaccomplished by assigning to each site visitor a unique identifying attribute, known as the session ID(SID), and then correlating that SID with any number of other pieces of data, be it number of monthlyvisits, favorite background color, or middle name—you name it. In relational database terms, you canthink of the SID as the primary key that ties all the other user attributes together. But how is the SIDcontinually correlated with the user, given the stateless behavior of HTTP? It can be done in two ways:367

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!