Has anyone created a PHP Session-like class in user code (not native)?

拈花ヽ惹草 提交于 2019-12-23 08:54:48

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/6061499/has-anyone-created-a-php-session-like-class-in-user-code-not-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!