note 92830 deleted from ref.session by danbrown
| From: | danbrown@php.net | Date: | Tue, 11 Aug 2009 15:22:07 +0000 |
| Subject: | note 92830 deleted from ref.session by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-159330@lists.php.net to get a copy of this message | ||
Note Submitter: jarrod at squarecrow dot com
----
I remember being confused by sessions when I first started programming so hopefully this simple
class will help someone in clarifying a few points for them. I've found that the class
structure is much smoother and lends itself to cleaner code than declaring $_SESSION over and over
throughout your application. An object-oriented approach to handling your sessions will save you
time and lend itself to more readily understandable code.
<?
class SessionManager
{
var $value; //multi
//CREATE OR UPDATE A NEW SESSION VARIABLE
function ModifyData($setKey, $setValue)
{
//Keys need to be strings
if (is_string($setKey))
{
$_SESSION[$setKey] = $setValue;
}
else
{
die("setKey must be a string!");
}
}
//REMOVES SESSION DATA FROM A SPECIFIC KEY - MUST BE STRING
function ClearData($setKey)
{
if (is_string($setKey))
{
unset($_SESSION[$setKey]);
}
else
{
die("setKey must be a string!");
}
}
function GetData($setKey)
{
if (is_string($setKey))
{
$this->value = $_SESSION[$setKey];
return $this->value;
}
else
{
die("setKey must be a string!");
}
}
}
?>
Then, in your code....
<?
session_start();
include("session_manager.php"); //assuming you save your classes in seperate files
$sm = new SessionManager;
$sm->ModifyData("USERNAME", "jHancock"); //where USERNAME is the key to that
particular entry and jHancock is what is stored in USERNAME
//Grabs the data using your key
$sm->GetData("USERNAME");
//Removes a particular entry using a key
$sm->ClearData("USERNAME"):
?>
Another good approach in session handling is to separate your session keys into three groups -
Context, Temporary, and Volatile - especially if you're working large datasets and you are in
and out of the session non-stop.
Create a class that on key registration tacks on a "_c" "_t" or "_v"
to each session key and then maintain a list of all items currently in the session.
Context -> Persistent data that stays stored in the session (such as authentication).
Temporary-> Data that only needs to be stored for a short time, such as an ID needed to grab
information from a database or saving page information to be displayed later.
Volatile-> Data that only needs to stay available for as long as the current page is open.
With your session keys divided into three groups, establish rules and procedures on page loads to
handle the dismissal of these groups.
Example -> On page load loop through each key in the Volatile group and unset each key.
Example -> Establish a rule (ex: user navigates to a different section of the site) that triggers
the dumping of the Temporary session group.
Example -> Empty the Context group on logout.
Obviously these will need to be tailored to individual uses but overall the practice will lead to a
much more efficient session structure and prevent session data from piling up on your server, which
can be a huge problem when dealing with thousands of concurrent users.