Session Manager
Posted: Tue Dec 01, 2009 12:14 pm
This is a Session Manager that I came up with to try and manage our sessions more efficiently. It works by separating the session into "pools" (groups) that can be manipulated individually, rather than effecting the session as a whole. My projects frequently involve recycling portions of the session as events occur (I'm using this plugged into my MVC framework, where I believe it has the greatest potential since it allows for stronger event-driven programming).
For example, a user logs in an is registered with the session as "username" but also with a whole host of other necessary data that I store to save making a trip to the database on every page. This data should persist throughout the term of the login and doesn't need to be touched until they log out. I call this the "Application" pool. (You can any number of pools, and of any name so long as they are in legal variable format - each pool is declared as a constant.)
However, there are eight major components to the application, each of which requires some interaction with the session at some time or another. Each component can be assigned its own pool in the session, which I call "Context". A user enters a component and a pool is set aside for handling all session data within that context. More importantly, once they leave that component I can establish a rule that recycles that pool alone, leaving my application pool and any other pools untouched.
Here's the class. I'm hoping some people will have suggestions/criticisms that I can use to make this better! Its already helped us immensely but I'm sure there's stuff I've missed.
Example.
For example, a user logs in an is registered with the session as "username" but also with a whole host of other necessary data that I store to save making a trip to the database on every page. This data should persist throughout the term of the login and doesn't need to be touched until they log out. I call this the "Application" pool. (You can any number of pools, and of any name so long as they are in legal variable format - each pool is declared as a constant.)
However, there are eight major components to the application, each of which requires some interaction with the session at some time or another. Each component can be assigned its own pool in the session, which I call "Context". A user enters a component and a pool is set aside for handling all session data within that context. More importantly, once they leave that component I can establish a rule that recycles that pool alone, leaving my application pool and any other pools untouched.
Here's the class. I'm hoping some people will have suggestions/criticisms that I can use to make this better! Its already helped us immensely but I'm sure there's stuff I've missed.
Code: Select all
<?php
/**
* This is a more advanced session manager that groups session data into logical pools.
* Data is grouped into session pools - technically you can have two identical keys
* that point to different pools. For example if you have the key "user" for the Context session then you can
* also have a "user" key that points to the Application session.
*
* The idea behind this is to properly manage sessions without a timer or waiting on the session cache to expire.
* By effectively using the Recycle function, you can create conditions for when certain pools are recycled to separate
* permanent, temporary, and volatile data.
*
* @author Jarrod Nettles - 2009
*
*/
class SessionManager
{
/**
* Starts the session and retrieves any existing pools.
* @return void
*/
public function __construct($session_start = false)
{
if($session_start == true)
{
session_start();
}
$this->retrievePools();
}
/**
* Starts the session, if it hasn't already.
* @return void
*/
public function StartSession()
{
session_start();
}
/**
* Pass a comma delimited string of the pool names you need to create. Creates a constant for you to use
* when referencing the pool later.
* @param $pools String
* @return void
*/
public function CreatePools($pools)
{
//register the available pools in the session so that they can be retrieved at any time
$_SESSION["session_pools"] = $pools;
//break the pool list into an array
$pools = explode("," , $pools);
//create a constant for each pool provided
foreach($pools as $item)
{
define(trim($item), trim($item));
}
}
/**
* Retrieves the existing pools from the session - called on class initialization.
* @return void
*/
private function retrievePools()
{
if(isset($_SESSION["session_pools"]))
{
$this->CreatePools($_SESSION["session_pools"]);
}
}
/**
* Retrieves data from a session pool based upon its key.
* @param $key String
* @param $type Constant
* @return unknown_type
*/
public function GetData($key, $type)
{
return $_SESSION[$type . "_" . $key];
}
/**
* Creates or modifies data in the provided session pool.
* @param $key String
* @param $value Any
* @param $type Constant
* @return void
*/
public function ModifyData($key, $value, $type)
{
$_SESSION[$type . "_" . $key] = $value;
}
/**
* Empties an entire session pool.
* @param $type Constant
* @return boolean
*/
public function Recycle($type)
{
try
{
foreach($_SESSION as $key => $value)
{
$check = explode("_", $key);
if($check[0] == $type)
{
unset($_SESSION[$key]);
}
}
return true;
}
catch(Exception $e)
{
return false;
}
}
}Code: Select all
<?php
$sm = new SessionManager(true);
$sm->CreatePools("Application, Context");
$sm->ModifyData("username", "JNettles", Application);
$sm->ModifyData("email", "JNettles@company.com", Application);
$sm->ModifyData("ACL", 5 , Application);
$sm->ModifyData("username", "Jarrod", Context);
$sm->ModifyData("contact_id", 5476880, Context);
$sm->ModifyData("application_id", 43454748, Context);
$sm->ModifyData("return_page", "articulations", Context);
echo $sm->GetData("username", Application); //echos "JNettles"
echo $sm->GetData("username", Context); //echos "Jarrod"
//recycle our Context pool (unsets the entire pool)
$sm->Recycle(Context);
//Application pool is untouched
echo $sm->GetData("ACL", Application); //echos "5"
?>