Manual for the DB_eSession PHP Class (ver 1.0.2) |
Table of Contents
Take a look at the readme file for quick installation steps. Disclaimer/License This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the: Free Software Foundation, Inc., Section 1 - Introduction.This manual describes the use of a great custom PHP session handler class called DB_eSession. This free PHP class allows the storage of session data in a MySQL database rather than files (which is PHP's default). By storing PHP's session data in a database, it allows you to have better control of what information is stored, how it's retrieved or verified and makes for tighter security. DB_eSession contains a lot of practical features designed for real world use where tougher security measures are essential. The class and its list of feature parameters may seem a little overwhelming at first glance, but it is really easy to use. Every field parameter has a set default, so you don't have to necessarily fill-in a value for every feature parameter to use the class. There is a table provided that describes each field parameter the class supports and its use. This class includes member functions useful (to web masters) for monitoring/viewing, deleting, and altering sessions validity, like in the case of locking one or more sessions upon detection of unauthorized use. This custom PHP/MySQL database session handler class might just be what you're looking to implement on your web or intranet site. You can use your browsers find command to search for what you're looking for in this manual. When you want to print this manual, it's best to choose the landscape mode with half inch margins all around. Please feel free to send me an e-mail to register that you're using this class. That way, you will be notified if there's a new release of this class. Send e-mails to: code@dearneighbor.com Thank you and enjoy,
Section 2 - Features.The DB_eSession class has the following list of features: General features:
Security features:
Other features:
Section 3 - System Requirements.PHP version 4.2.0 or higher, MySQL version 3.22.5 or higher, web server (Apache, IIs), and Unix/Linux/Windows/Mac. An ability to create MySQL databases, usernames and passwords such as with phpMyAdmin. libmcrypt 2.2.x or higher is optional if you want to utilize encryption/decryption. The included sessEncode/sessDecode member functions will automatically be utilized when libmcrypt is not installed and encryption is requested. PHP's register_globals setting can be on or off, and the magic_quotes_runtime is automatically handled. The magic_quotes_gpc is checked but this class doesn't know if you will be trying to save session data coming straight from Get/Post/Cookie variables. This class by default will addslashes() to session data before saving, therefore it's best for you to stripslash() if necessary before hand. Session.auto_start needs to be off.
Section 4 - Assumptions.This manual was written with the assumption that you know at least the basics of the PHP language and it's syntax. You don't have to be an expert, however, a good understanding of how to create and use variables (especially arrays) is essential. You don't need to know a lot about classes, but it would be enough to know how to create a new instance and reference them (there's examples given in this manual and the example files included). You should be able to know how to create PHP text files and upload/install them onto your server or web host provider, and be able to set the correct file permissions if necessary. There's no need to know about PHP's session mechanism but a general understanding of what sessions are for would be useful. The class takes care of the SQL queries, so there's really no particular need to know MySQL or SQL syntax, but knowing the basics would be helpful. It is at least required that you know how to create a MySQL database, table, a username, and password using any of the tools at your disposal (such as phpMyAdmin). Only the SQL to create the MySQL session table is included.
Section 5 - How to Install.If you downloaded the compressed file, then you'll need to uncompress the file to reveal/extract all the individual files contained within it. You can use the tar command with the '--extract' option for Unix/Linux type of systems, WinZip for Windows, or StuffIt (Standard Edition Trial Version) for the Macintosh (which will uncompress the Windows zipped file under the Mac). Please refer to these tools manuals for how to extract the necessary files out of the compressed file. Use phpMyAdmin or similar tool to create a MySQL database, username and password. If you already have these defined/setup, then you can use these if you wish instead of creating new ones. For security reasons, it is recommended for you to create a separate database, a restricted username, and password just for the sessions processing. If an unauthorized intruder gained access to the sessions database, then they will hopefully only have access to the sessions table, while your application related tables are perhaps protected by being in another database under a different username and password. Use the SQL within the included 'eSessions_table.sql' text file to create the sessions table (using phpMyAdmin). Then don't forget to update the 'config.DB_eSessions.php' file (discussed below) with the name of the local host, database, username, password, table name, and column names if they are different than the supplied defaults. There are two required PHP files needed for a successful installation and one optional configuration file. The main PHP class file is called 'class.DB_eSession.php'. This class requires an errors include file which comes with the package and is called 'errors.DB_eSession.php' by default. The third and optional configuration file is called 'config.DB_eSession.php'. This is not required by the class, but is made available to you for placing along with the scripts you write. It's recommended you keep the same naming conventions, that way all your class, configuration, and error files will be grouped together when viewing your directory listing (where the files are contained). Before installing these files you might want to think about how you should organize your application directories on the web server. The files that come with this package do not need to be in the public web directory. It's actually best to keep them off the public web directory for security reasons. You only need to place the script(s) that the user interacts with in the public web directory, and have those scripts point to the directory where the other include files are stored. Let's go through an example. Say we have a root path of /home/user and a public web directory path of /home/user/www. One approach would be to create a PHP folder/directory used as a repository to hold all the files (such as those included in this package) that should be protected or just generally not needed in the public web directory area. You could call it something like 'php' or 'phplib' and create it in the user root area (i.e. /home/user/phplib). Then in your script, specify a relative or absolute path when using the include or require commands. In the following example it is assumed that the script is running from the /home/user/www public web directory: <?PHP Another option is to specify all the various paths you have into the 'include_path' configuration setting. This causes PHP to search in all the directories specified for the file that you're trying to include/require. If you can't change the paths in your php.ini file, then you'll have to set the 'include_path' in every script that uses an include/require. Below is an example of how to change the 'include_path' configuration setting from within a PHP script: <?PHP The above code could be put in your own 'config' file and placed in the public web directory with your application script. Your application script can simply include the config file without specifying any paths since the config file is in the same directory as your application script. Optionally, you can also add the creation of the new instance of the class in your application config file. Below is an example of how the start of your /home/user/www/index.php script could look like: <?PHP This class also uses an error include file called 'errors.DB_eSession.php' which is specified by setting the 'errors_path' and 'errors_file' parameter options. You can call this file something else, but it must be saved with '.php' at the end. When specifying 'errors_file' parameter, don't include the '.php' at the end of the filename because the class will add that automatically. For security reasons it is highly recommended that all your PHP scripts end with '.php' no matter what they are (config, application scripts, errors, classes, etc.). After modifying any necessary files (i.e. config or errors), the files need to be installed or uploaded to your web server or web host provider (for uploading use an FTP software tool). Here is an example of how your files could be organized after an upload: /home /user /data /phplib /www
Section 6 - How to invoke the class and pass parameter options.You need to create a new instance of the class right at the beginning of your PHP script(s) (or at least before you send any output to the browser). You can have other include files first (before 'class.DB_eSession.php'). Again, just make sure there are no output sent to the browser before the creation of the DB_eSession instance, otherwise PHP will give you a 'headers already sent' error message. If you get this error, remove all whitespaces from include files or any echo/print commands invoked before creating an instance of the DB_eSession class. There can't be any whitespaces before the '<?PHP' at the start of your script or in include files. There also can't be any whitespaces after '?>' at the end of your script or in include files. If you're still getting this error after checking and fixing all your script/include files, then you will have to use PHP's output buffering option. This can be easily accomplished by passing the 'buffer' parameter set as TRUE to the DB_eSession class. The class will then know to invoke the PHP ob_start() function. Make sure that the main class include file is placed at the top of your script(s) (see suggestions supplied in Section 5 - How to Install). The class can be invoked in three different ways. The easiest way is without passing any parameter options. But this only will work if you created the MySQL database, table, username, and password the same as the defaults defined in this class (see list of parameter options section). Below is an example of how to invoke a new instance of the class without passing any parameters: <?PHP The second way is by passing a non-variable associative array of parameters defining the options you would like changed. Below is an example of that: <?PHP The third and most recommended way is by using an associative array variable. You can create your own variable or use the supplied configuration file like in the following example: <?PHP The $sess_param associative array variable is defined in the 'config.DB_eSession.php' file and it's passed as an input/argument to the DB_eSession class. You could have one main configuration file (recommended) that is used by all your scripts or you could have many. If you need to change an option for a particular script, you could override the configuration file setting by specifying that after the configuration include and before the creation of the new instance, like this: <?PHP The output buffering option is normally set to FALSE (off) in the configuration file, so the above example is turning it on (TRUE) before the class instance is invoked.
Section 7 - A list of parameter options supported by the class.Below is a table containing a list of all the various parameter options this class supports. The 'Array Key Name' column is the name of the key used in the associative array passed when creating an instance of DB_eSession class. For example: This class supports the setting of Session Runtime Configuration options. Note that not all Session Runtime Configurations can be set or take effect outside of the php.ini configuration file. Some options can be set at runtime without an error produced, but have no effect. (i.e. session.auto_start). All session options are used here in case there is future support to make an option take effect at runtime (i.e. session.use_trans_sid). Don't add the 'session.' part to the array key because the class automatically adds that. For configuration option details, see: http://us2.php.net/manual/en/ref.session.php. The 'R/O' column means whether the parameter is Required or Optional. Required means that it is most likely that you might want to change the value, to make it different than the class default, before creating an instance of the DB_eSession class. |
|
|
|
Section 9 - About the errors/warnings include file.The 'errors.DB_eSession.php' file is used to define all the error and warning messages that this class needs. A two dimensional associative array is used to store the error messages, and another to store the warning messages. The first array index is an alphabetic code to identify the error/warning and it must not be changed because the class will be looking for these already predefined specific values. The second index of the array is a two character alphabetic code that identifies the language, and this can be changed to suit your needs. You must remember to assign the default language code and the current language code to match your error/warning language code definitions before creating an instance of the class. The error array variable name must be called $_ERR and the warning array variable name called $_WRN. For example: $_ERR['SELECTDB_FAIL'] ['en'] = "Could not select MySQL database: %s\n"; $_WRN['NEW_SESS_ID'] ['en'] = "Assigning a new session ID.\n"; As part of the error/warning message there maybe strategically placed '%s' markers. These act as data place holders. The class will automatically replace these '%s' markers with actual values that pertain to the error/warning message. For example: in the 'SELECTDB_FAIL' error above, the '%s' will be replaced with the database name that it tried to select (if the detail error report setting is on, otherwise it will just show '[xxx]'). Leave these place holders, especially when creating error/warning messages in new languages. To create messages in another language other than English, copy all the English errors and warnings and replace the 'en' value (the second index of the array) with another code, like 'fr' for French, 'gr' for German, or 'sp' for Spanish, etc. Then, translate each error and warning message into that language while leaving the data place holders ('%s') in the right places to coincide with the languages proper grammar. An example of how to create error and warning messages in another language is given inside the 'errors.DB_eSession.php' file. Note: The letter 's' used in the data place holder can be in upper or lower case.
Section 10 - Recommendations and things to watch out for.In general, it's recommended to keep register_globals configuration setting off. Have E_NOTICE error_reporting() setting in effect while you test your scripts. It helps in identifying various problems in code including unassigned values, finding (variable) typos, and will warn you about bad style. Remember to remove the option in a production ready site.
Section 11 - Known ProblemsWhen the 'stop_on_error' or 'stop_on_warn' parameter options are on, and an error/warning occurs within the class, the class will display the error/warning and stop the script. However, PHP might decide to display a warning message similar to the following as well: There are a couple of bug reports posted regarding this. See: http://bugs.php.net/bug.php?id=26005 and http://bugs.php.net/bug.php?id=21306. The suggested fixes in these bug reports have not worked in my testing. This warning seems to happen when the class does the exit, but not when the exit occurs outside of the class like within your script. Turning off warnings using error_reporting() does not get rid of this warning message either. This clearly indicates a PHP bug. If you should get this warning and you want to get rid of it, a workaround for now is to turn off 'stop_on_error' and 'stop_on_warn' options (which is before a new instance of the class is created), and check at strategic places for the errors and warnings from within your script. Like in this example: if ($sess->errorsExist()) { if ($sess->warningsExist()) { You can also use endStopOnErrors() and endStopOnWarnings() after a new instance of the class has been created to turn off errors and warnings from being generated from within the class.
Section 12 - Revision HistoryRevision 1.0.1 - June 3rd 2004: The sessDecode member function sometimes truncated the last character. The bug had occurred when all of the following conditions were met:
The problem was noticed when getSessVar was used to retrieve the text, which would contain one less character at the end. When the extra field option was used in setSessVar, then getSessVar returned FALSE. To simply this: Also, this manual was revised to include this revision section. Revision 1.0.2 - August 8th 2004: The sessDecode member function is missing an initialization line for the $_decoded variable. This may lead to unpredictable return value results. The fix involved changing this area of code in sessDecode, from this: $_array = array_flip($_array); To this, which initializes $_decoded to an empty string: Also, the $_SERVER['HTTP_ACCEPT_ENCODING'] value returned by Internet Explorer v6 was found not to be consistent especially when dealing with secure pages (HTTPS). Since this value was used as part of determining if it's the same user accessing the session, this could lead to the class not finding the same session table row. The fix was to simply comment out related code in the _getSecID() member function like this: /* Rev 1.0.2: Found IE 6 to not return consistent results (under HTTPS) This manual was revised to include revision 1.0.2 details.
End of Manual.
|