[Package Proposal] PHP_Application_Vars
| From: | Davey | Date: | Sat, 23 Aug 2003 17:08:18 +0000 |
| Subject: | [Package Proposal] PHP_Application_Vars | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-20442@lists.php.net to get a copy of this message | ||
OK, I just kinda whipped this userland code up to implement something I've been thinking about for *months*.
I proposed my idea for easy, very functional, etc etc $_APPLICATION vars to internals some months back, but there was some apprehension and because I don't know C, I couldn't provide some code.
Anyways, I've adapted this idea and brought it to userland code.
Heres how it works.
When you instantiate a copy of PHP_Application_Vars you can provide an optional application name. Now, what happens is)
If you provided an application name then this is used, if not it uses $_SESSION['SCRIPT_FILENAME'], this value is used as sha1'ed as our session id for the application vars. If you don't provide this application name, then the application vars will persist ONLY for this script, any scripts with the application name will have the application vars across all of them.
Now, this works like this:
Application vars go in a special session array index, ASID (Application Session ID), this is a sha1 hex number, so you use $_SESSION[ASID] for all your session vars.
Standard session vars are written to:
$_SESSION[USID] - where USID is a unique id for each user, again a sha1 hex number, what happens is, when I write the session to the session file, it moves everything from $_SESSION[*] to $_SESSION[USID], we have two problems: firstly, the ASID is there, so we skip that, secondly all the other USIDs are stored in this same session, so we skip this using a preg_match of /[0-9a-f]{40}/ which means the user cannot put their data in a session var with that a key that matches that (thats one reason I chose sha-1, less used, they can still of course use md5 :)
Now, this means that all the users sessions are stored in a single session, when we read the session, we move everything back from $_SESSION[USID] to $_SESSION so the author of the script doesn't have to remember to use $_SESSION[USID]['foo'] :)
I have one problem with this... session vars will only persist across applications (on a per-user basis though). So a) does this need to be fixed? b) how could it be done? can't have two sessions (application vars and session vars) :/
I think this could turn into a very elegant solution...
Anyhow, I have one last problem, the code has a bug I cannot find. I'm gonna put my links to source, package, et al; any help would be appreciated :)
PHP_Application_Vars 0.1:
http://davey.is-a-geek.com/php-cvs/pear/PHP_Application_Vars/PHP_Application_Vars-0.1.tgz
PHP_Application_Vars 0.1 Source:
http://davey.is-a-geek.com/php-cvs/pear/PHP_Application_Vars/Application_Vars.phps
My first test, shows the error:
http://davey.is-a-geek.com/php-cvs/pear/PHP_Application_Vars/tests/test1.php
This accompanies test1.php, should display the application/session vars, shows an error also:
http://davey.is-a-geek.com/php-cvs/pear/PHP_Application_Vars/tests/test2.php
Anyways, hope to hear feedback/bugfixes on this.
- Davey