$_SESSION['username']." logged on at ".$_SESSION['loggedon'].".";
?>
This returns:
User jason logged on at Feb 16 2008 22:55:22.
This hypothetical example is intended solely to demonstrate PHP??™s session encoding
and decoding function. If you would like to store session data in a database, there??™s a
much more efficient method that involves defining custom session handlers, and
tying those handlers directly into PHP??™s API. How this is accomplished is demonstrated
later in this chapter.
Practical Session-Handling Examples
Now that you??™re familiar with the basic functions that make session handling work,
you are ready to consider a few real-world examples. The first example shows you
how to create a mechanism that automatically authenticates returning registered site
users. The second example demonstrates how session variables can be used to provide
the user with an index of recently viewed documents. Both examples are fairly commonplace,
which should not come as a surprise given their obvious utility. What may come as
a surprise is the ease with which you can create them.
?– Note If you??™re unfamiliar with the MySQL database and are confused by the syntax found in the
following examples, consider reviewing the material found in Chapter 30.
CHAPTER 18 ?– SESSION HANDLERS 459
Automatically Logging In Returning Users
Once a user has logged in, typically by supplying a username and password combination
that uniquely identifies that user, it??™s often convenient to allow the user to later
return to the site without having to repeat the process.
Pages:
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540