Manual v1.x : Table of Contents
Resession v1.9 (Log)
Uploaded: 07/18/2009
Requires: PHP 5
Tested On: PHP 5.2.5
View Manual:
Resession
Package: Session Manager
Category: PHP & MySQL
Views: 4,605
A small lightweight script that can manage and manipulate Session data. Calls session_start() in memory so that no header errors are thrown, as well as stores the session id in the object.
Resession is a play on words for: Session Request + Response.
Class Features:
Resession is a play on words for: Session Request + Response.
Class Features:
- Calls session_start() automatically in memory
- Modifies the ini settings for extra security and protection
- Uses http cookies to store session data
- Get and set data as a string or array
- Clear a session key, or destroy the session
- Get and regenerate session ids
- Redirects to another page
- Can set raw cookies
- Security levels of high, medium, and low
- Configuration settings
Top
1 - How It Works
The Resession class is pretty simple in that it uses a Singleton pattern to store the object into memory. By doing this, we do not need to place session_start() at the top of our PHP files and it stores a persistent session id. It also does not throw any header errors caused by session_start() (Not entirely sure, but I have yet to run into the problem).
Top
2 - Configuration
In the 1.9+ version, you have the ability to configure how Resession works. There are 3 configuration options which deal with your security, how cookies are used in conjunction with the session and the sessions name. To edit the options, you would call the new config() method (to make sure it works correctly, you should call your config at the top of your scripts).
Now let me quickly explain what these different options do, but it would be best to look over the script yourself to get a good understanding of it.
Alternatively you can pass an array as the first argument to config(), instead of calling it many times.
Session::config('security', 'high'); Session::config('cookies', true); Session::config('name', 'resession');
Now let me quickly explain what these different options do, but it would be best to look over the script yourself to get a good understanding of it.
- security - You can set your level to high, medium (default) or low. If set to high or medium, the script will enable referrer checking, force the cookies on a secure http connection and set a timeout for cookie lifetime. If set to low, none of these will be enforced.
- cookies - If set to true, it will force the script to use cookies to store session information instead of applying a session id to the urls.
- name - You can set the name of your session id, session name and cookie name. By default its named "RESESSION"
Alternatively you can pass an array as the first argument to config(), instead of calling it many times.
Session::config(array( 'name' => 'RESESSION', 'cookies' => true, 'security' => 'high' ));
Top
3 - Manipulating Data
Setting Data
The most common use for sessions, is to set data to the session. To do this we use the set() method. You may set data to a stand alone index, or to an index within an array by using a dot notation. The dot notation only works for single dimensional arrays.
Retrieving Data
To get data from a session, you would use the get() method. The same concept of array dot notations applies to this method. (In the example below, I will be referencing the $data array above).
The most common use for sessions, is to set data to the session. To do this we use the set() method. You may set data to a stand alone index, or to an index within an array by using a dot notation. The dot notation only works for single dimensional arrays.
$data = array( 'name' => 'Miles', 'website' => 'http://www.milesj.me/' ); Session::set('user', $data); // Change the name index within the user array Session::set('user.name', 'Miles Johnson');
Retrieving Data
To get data from a session, you would use the get() method. The same concept of array dot notations applies to this method. (In the example below, I will be referencing the $data array above).
// Get the user array Session::get('user'); // Get the users name Session::get('user.name'); // Miles Johnson
Top
4 - Clearing Data
If for some reason you need to remove an index from the session, you can use the clear() method. Dot notations apply to this method, as well you can pass an array of indexes to remove.
Destroying the Session
If you need to delete all data in the session entirely, you would call the destroy() method.
// Set fake data Session::set('time', time()); Session::set('date', date('m/d/y')); Session::set('user', $data); // Clear data Session::clear('time'); Session::clear('user.name'); // Alternative Session::clear(array('time', 'date', 'user.name'));
Destroying the Session
If you need to delete all data in the session entirely, you would call the destroy() method.
Session::destroy();
Top
5 - Accessing the Session ID
If you need to access the session id that is stored in the class, you can use the getId() method.
Expanding this further, if you need to regenerate a new session id, you would call the regenerate() method and the new session id will be returned. You can also pass a boolean true/false argument to clear the old session file (more information here), by default it will remove the file (true).
$session_id = Session::getId();
Expanding this further, if you need to regenerate a new session id, you would call the regenerate() method and the new session id will be returned. You can also pass a boolean true/false argument to clear the old session file (more information here), by default it will remove the file (true).
$new_session_id = Session::regenerate(); // Do not delete old session data $new_session_id = Session::regenerate(false);
Top
Read the whole documentation? Download the script now and try it yourself! Return to the Top
6 - Special Methods
The class comes bundled with two additional methods that aren't session related, but can be very helpful. The methods are redirect() and setCookie(). The redirect() method does exactly as its name suggests, it redirects to another page.
The second method setCookie() works exactly like PHPs built in setcookie(), but instead sets a raw cookie that would be more useful in certain situations. The arguments are also identical to setcookie()'s.
Session::redirect('/index.php');
The second method setCookie() works exactly like PHPs built in setcookie(), but instead sets a raw cookie that would be more useful in certain situations. The arguments are also identical to setcookie()'s.
// Expires in 30 days Session::setCookie('cookieName', 'resession', time() + (60*60*24*30));