7

The native PHP Session functionality is great, but it's ultimately a singleton. There are times when you need to maintain state for multiple apps and in the scope of an already-started session (e.g. in an app framework). Technically one can stop/restart a session after changing session_name(), but this is impractical/impossible/unsafe within most apps. Using a shared session.save_path is also not an option if one app stores session data with a non-disk adapter.

There's no reason the functionality in native sessions can't be done in user code, so has anyone done this?

Update 1: CI_Session is indeed a userland implementation with some useful code, but it's highly coupled to CodeIgniter.

Update 2: Here's an API that would be great:

// setup
$saveHandler = new UserlandSession_SaveHandler_Files('5;/tmp');
$sess = new UserlandSession($saveHandler);
$sess->name('PHPSESSID2');
$sess->gc_maxlifetime = 86400;
$sess->setProxy($state); // passed by ref
// usage
$sess->start(); // stored string unserialized to $state
$state['foo'] = 'bar';
$sess->write_close(); // $state serialized to storage

Update 3: I've written an implementation for PHP5.3.

Steve Clay
  • 8,671
  • 2
  • 42
  • 48
  • I believe code igniter does such thing. – Amir Raminfar May 19 '11 at 16:04
  • 2
    You can create your own session handler system using http://php.net/manual/en/function.session-set-save-handler.php, using any kind of back-end storage system you want. – Marc B May 19 '11 at 16:06
  • Do you want to share single session in more that one app? – Shakti Singh May 19 '11 at 16:07
  • +1 Thanks for raising this question - and any answers that may appear - this is great functionality I never thought of. – Bojangles May 19 '11 at 16:12
  • @Marc B: You can't have two sessions active simultaneously is the problem. – Steve Clay May 20 '11 at 02:13
  • @JamWaffles: Besides the practical use, a userland implementation would also give insight into how native sessions actually operate. – Steve Clay May 20 '11 at 02:23
  • @mrclay: true, but once you're rolling your own handlers, no reason you can't store things in a different session variable. You just lose the benefits of having a superglobal available throughout your code. – Marc B May 20 '11 at 04:25
  • @Marc: That's the only issue I can see with RYO - scope. Other than that, it's wonderful due to the fact you can store stuff in a database or whatever. Maybe writing a wrapper for `$_SESSION` would be a good idea? – Bojangles May 20 '11 at 07:40

2 Answers2

2

CodeIgniter has a session class that does not utilize native PHP sessions.

Sean Walsh
  • 8,266
  • 3
  • 30
  • 38
0

I wrote UserlandSession in response to this.

It's a pure PHP implementation of "sessions" that can be used to bridge a session between arbitrary PHP apps. It does not interfere with native sessions, has an OO storage API (more like PHP 5.4), and has an API similar to native sessions.

It comes with filesystem and PDO storage handlers and an interface to make it easier to write your own.

Steve Clay
  • 8,671
  • 2
  • 42
  • 48