|
IntroductionPHP 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:
Out Of The BoxThis 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!
This would produce a much more robust PHP Session Manager Installation & RequirementsRequires 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 UsesCreate 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() ); */ Assigned TagsLanguages: 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 FollowingComments
there are 1 subscribed coders |