Author: Troy Wolf (troy@troywolf.com)
Modified Date: 2005-06-18 14:20
Download: class_session.zip
View class source: class_session.php source
class_session is a session management and password protection class. It can be used to perform 2 major functions:
This is accomplished by storing a single session id cookie on the user's computer. No other data is stored on the user's computer, and the cookie expires when the user closes their browser. Because PHP can't write cookies after browser output is started, make sure you create the session and save session data BEFORE you start any browser output.
Before you can use the class, you need to modify the 'dir' property in the class file to point to a directory where you want to store session cache files. You'll find this property in the session() function (the class constructor).
To use the class in your scripts, you first need to include the class file.
Next, you instantiate the session object. Do this in every page you want to either save or restore the session in. (Or password-protect, but we'll talk more about that later.) The code example below also shows how to echo the session's 'log' property which will tell you about what's wrong if things don't work. NOTE: Create your session and save any session data BEFORE you start any browser output, or this will not work.
Now that you have a session, you can save data in it. Suppose the user submitted a form to your script with their name and favorite color. Again, we want to test if anything fails. If the save() method returns false, we'll look at the session's 'log' property.
If everything worked, there will be a SID (session ID) cookie on the user's computer and a session cache file on your webserver's hard drive with the same name as the SID.
Now you have a way to maintain state specific to this user. You can access this session data in all the rest of your scripts. With the class, you don't do anything special to restore the session--simply create a new session object, and if a session exists, it will be restored. Here is a complete code example showing a script that restores a previously created session and uses the session data.
The class also provides an expire() method to end the session. You can use this with a "logout" feature.
View the source for the example logout.php.
Hopefully you now understand how easy it is to use class_session to maintain user data between pages. View the source for example.php which puts this all together for you, and is included in the download.
Another powerful feature of session_class is the ability to password-protect pages. The class uses a session variable 'logged_in' to keep track of whether the user is logged in or not. Let's walk through how to use this functionality.
Instantiate the object like before, but this time, pass 'true' to the constructor. The class will then test the 'logged_in' session variable. If it is false, it will "remember" where the user was going, then redirect to your login page.
So, if the user hit your page and was not logged in, they would be directed to your login page. This can be any page you want--you define this in the class by setting the 'login_page' property in the session() function. It defaults to login.php. An example login.php is included in the download. You can modify this login.php or create your own, but there are a few pieces in it that you must keep to make it work with class_session. You can view the source of login.php.
Remember, you can use the expire() method to end the session--which effectively logs the user out. Alternatively, if you wanted to continue to maintain the session, but only log the user out from access to protected content, you can just set the 'logged_in' session variable to false.
Troy Wolf operates ShinySolutions Webhosting, and is the author of SnippetEdit--a PHP application providing browser-based website editing that even non-technical people can use. Website editing as easy as it gets. Troy has been a professional Internet and database application developer for over 10 years. He has many years' experience with ASP, VBScript, PHP, Javascript, DHTML, CSS, SQL, and XML on Windows and Linux platforms.
My logo was designed by Rus Anderson, a skilled graphic artist with a wealth of experience in user interface and web design. I told Rus I wanted something simple and clean, that conveyed my expertise in document automation technologies. I also wanted an association with PostScript and PDF. I'm very pleased with the result. The "document icon" has become ubiquitous. It's obvious, when viewing the logo, that I'm involved in document production and automation. The red color is associated with Adobe, PostScript, and PDF. Overall, the effect is clean and memorable. Thanks, Rus!