Scott Thompson

I have 22 followers

Download Source Code (6kb)

Version 1.00.00 Beta licensed using
"Creative Commons: Attribution (3.0 Unported)"

Introduction

PHP Session Manager allows you to completely ignore the very primative session system provided by PHP out of the box. With the set of classes provided by PHP Session Manager you will be able to:

  • Obtain complete control of the session process (creation, management, deletion) with much finer grained control than the custom session handlers provide in PHP
  • Remove the $_SESSION super global and no longer use horrible globals throughout your code resulting in more portable code
  • Support multiple sessions at once (eg, allow super fast memcache based sessions only for instant messenger users and file based for everyone else)
  • Transparently use, switch and even mix different session backends such as MySQL, Memcache, APC and File Based (and easily add more by just extending an abstract class)
  • Easily add new methods of passing a session id around stateless systems such as HTTP (currently supports cookie, but you can easily add more)
  • Easy to scale by simply extending the session storage class to work with what ever system you have in place such as distributed memcache servers
  • Easy to read code, complete with comments and sticking to many OOP principles such as DRY (Don't Repeat Yourself)
  • Ability to add IP session locking to prevent Session Hijacking attacks, encryption and more...
  • Auto saves the session data when the page finishes executing (can be turned off per session object and changed to manual control)
  • Easily scale your application as it grows by simply switching a class over. Start with session_storage_file_system for shared hosting, then move to session_storage_apc as traffic builds finally finishing with a distributed session_storage_memcache system or even create your OWN class to interface with another storage system!
  • 100% Object Oriented providing all the usual benefits of OOP

Out Of The Box

This is the first version i created after 3 hours of planning and 5 hours of coding. It currently only supports cookie based sessions using APC. I will eventually add database and others to the session storage engines but i don't have the time yet. If you would like to create any of the following classes please be my guest!

  • session_storage_db (using PDO to allow multiple database such as Postgre and MySQL)
  • session_storage_memcache
  • session_storage_file_system
  • session_transporter_url_params

This would produce a much more robust PHP Session Manager

Installation & Requirements

Requires PHP 5+ along with the dependencies of the storage engine you use such as if you use the APC engine you will need the APC extension installed.

To install simply copy the classes provided, load them into your project and use them like in the examples shown further down. There is also an example script in the source code download.

Sorry the documentation is pretty lacking, i will revamp it once i (or someone else) further develops this package and to be honest you will get a good understanding of how it all works by reading the comments in the abstract classes alone and looking at the examples.

Example Uses

Create a session & load the same instance in a function (removing need for globals)

//======================================================
//=== load a session using APC and passing the session id via cookies
//======================================================

$session = session::get_instance(
	"main",						//the instance of this session - providing the same instance name will ALWAYS return the same session object
	new session_storage_apc(), 		//used to construct the session (for more information, read the comments in the source code)
	new session_transporter_cookie()	//used to construct the session (for more information, read the comments in the source code)
);

//======================================================
//=== now save something in the main session
//======================================================

$session['names'] = array("scott", "tom", "ben");

//======================================================
//=== since $session_main is not a super global, and using globals is bad... what do we do?
//======================================================

function print_names() {
	
	//now we can use a shortcut, since the object has already been created, there is no need to pass the other objects anymore, just the instance name (null is returned if no object can be found)
	$session = session::get_instance("main");
	
	//print the names from the session
	print_r($session['names']);
	
}

//prints: Array ( [0] => scott [1] => tom [2] => ben ) 
print_names();

Create and use multiple sessions at once

//======================================================
//=== load an APC session
//======================================================

$session = session::get_instance(
	"main",
	new session_storage_apc(), 
	new session_transporter_cookie()
);

//======================================================
//=== load another APC session used just for an Instant Messenger system
//======================================================

$session_instant_messenger = session::get_instance(
	"instant_messenger",
	new session_storage_apc(), 
	new session_transporter_cookie("SESSID_2")
);

See if a new session has been created, if so, seed it with some preset values

//implement a custom setup if a new session is created
if ($session->status() == session::STATUS_NEW) {
	
	//these variables will be set if this is a newly created session
	$session['logged_in'] = false;
	
}

Extend & Change Storage Engine

//======================================================
//=== to extend this to support more storage engines such as files, mysql, memcache, etc, you need only extend the session_storage class, implement the abstract methods and use it when constructing the session eg:
//======================================================

/* 
$session = session::get_instance(
	"main",
	new session_storage_mysql("localhost", "username", "password", "database", "table"), 
	new session_transporter_cookie()
);
*/

Extend & Change Transporter

//======================================================
//=== to extend this to support another session transport mechanism such as passing a session id via the URL params then you just extend the session_transporter class, implement the abstract methods and use it when constructing the session eg:
//======================================================

/* 
$session = session::get_instance(
	"main",
	new session_storage_apc(), 
	new session_transporter_url()
);
*/
1 0
please help rate the author by liking / disliking their work

Assigned Tags

Languages: Php

Technologies: Abstract Classes, Aggregation, Apc, Open Closed Principle, Php Library Classes, Session Handling

Categories: Php Classes, Php Packages, Session Handling

More Source Codes By VBAssassin

Source Codes By Coders VBAssassin Is Following

Comments

This has some potential! Love how you designed it so you can change not only change where the session is stored like apc or mysql but you also allow the way in which the session id is passed around to be changed as well. Very good. Just need more backends to it so you can use file based sessions, database, and so on. What about trying to get it added to an open sourrce framework once more backends are added of course. It might be a way to help promote this place as well?

Posted 2 Days Ago by Corrupted reply
Login to subscribe to comments
there are 1 subscribed coders
Please login to post comments or register for free