PHPlib
Monday, March 22, 2010  
download | documentation | faq | changelog | resources 


search for in the  


previousPHP4 Sessions with PHPlib using session4.inc*
Authenticationnext

Last updated: Thu, 04 Jul 2002
view this page in Plain HTML

Many people say that we do not need PHPLib session interface anymore since session support is now built in PHP4. I have some objections.

The first is that many many applications was written using PHPLib, so the rewrite should be done.

The second is that the Session class is really very convenient way to manage sessions. And it, of course, could be used as a framework for session management, while using new PHP4 session API, that, of course, is much more fast than the old PHPLib Session API.

Moreover, there is a lot of stuff in the PHP session module that could be tweaked. You can tweak it using php.ini directives. You can also tweak it using session api functions. There are many of them. I doubt that it is convenient to call them explicitly on every page. PHPLib Session class can do it for you using user-supplied values, and one $sess->start() call can do all custom tweaking and start the session with the parameters you want.

The third is that PHPlib has always had session data storage abstraction. Native PHP4 session module gives you a choice to use either files or shared memory as the storage for session data. If you want to store the data anywhere else, you should supply your custom functions. PHPLib has had this functions for years - they are in CT_* classes. Why not use them as our custom storage containers?

So, given this arguments, we could talk about something that I call the 'session abstraction', similar to the DB abstraction. Using this class you can tweak almost every session parameter - cookie headers, cache management, session name, storage mechanism, url rewriting .... from one place - your custom Session subclass declaration. Of course, there would be some overhead compared to using PHP4 session functions explicitly. But I estimate that overhead as negligible, while the benefits are too lucrative :)

The new Session object is written as a wrapper over native PHP4 session handling functions. It was written with compatibility in mind, while some of the compatibility somewhere was sacrificed in favor of performance. The class can use either native PHP4 session storage (currently 'files' and 'mm' modules) or PHPLib custom storage containers, implemented in CT_* classes, that currently can store data in a SQL database, DBM files, LDAP directories, and anything else, if you provide necessary CT_Something class.

The main differences from previous PHPLib Session are the auto_init file use, session data format, serialize() and thaw(), use of page_close() and use of the User class.

The new Session the serialize() behavior is changed. Now serialize() is the wrapper over native session_encode(), that returns session data in a native serialize() format or WDDX format, corresponding to the php.ini's session.serialize_handler. It much more faster, then using the old serialize(), that, using recursive calls, produced plain PHP code that should be feed to eval() in thaw() then.

The $persistent_slots in objects that should be registered in a session are no longer respected - all the class properties are now saved with the session.

thaw() is used as the custom session read handler now. It does not actually 'microwave' frozen variables, it just pass serialized session data to the session engine. To reimport session data use unserialize() (which is a wrapper over the session_decode() itself). Register() does not fill $pt array, it uses native session_register() instead. The session data is actually a serialized representation of $HTTP_SESSION_VARS array. So, you can not register any class property without registering the class itself. Register() can register only global variables. E.g. the old Session registers $sess->in property as a marker, whether auto_init file was used or not. That is not possible with the new register(). (Auto_init issues will be covered shortly) and, as you could see, the new session data format is uncompatible with the old one. But I suppose this should not affect any PHPlib-based applications.

* Adapted from the original README_session4_custom by Maxim Derkachev <kot@books.ru>

Editors note: As an explaination to the confusion regarding two implementations of PHP 4 session class I quote the following from an email written to the PHPlib mailing list by Max

nrh> Also, can someone tell me what *exactly* max's aptches are
nrh> in the php-lib cvs tree?  I still haven't the foggiest what 
nrh> the frell is missing.  Begin to feel stupid, quite I do.

Well, it's a source of many troubles, misunderstanding and 
tricky "walkthroughs", so I'll try to explain what's exactly  
going on there. History first. More than a year ago there was 
a discussion in the list on possibility of adaptation of session 
class to PHP4 sessions. This brought to us 3 (three!!!) 
implementations of native sessions with  PHPLib API. The first was 
by Teodor Cimpoesu, who, as he noted, did a quick and dirty hack 
to show that this is possible. The class was mainly old session 
class with some parts changed to calls to PHP4 session API, the 
other parts (storage, SID propagation and such) just commented out.
The second was by Barendt Scholtus (excuse me, Barendt, if I 
misspell), who did the similar job to Teodor, but also changed
user.inc and page.inc in the similar way. He named his effort as
phplib4, and it remains by that name in the CVS now in the unsup
directory. Both implementations just used to be simple and limited 
wrappers over PHP4 session module, conforming to the PHPLib API.
Then, I introduced my session4_custom, which was almost fully
rewritten implementation, which was intended not only to conform to
the PHPLib (public) API and use native PHP4 session module, but also 
provided a custom save handler, which used PHPLib's CT_*-style 
containers as a session data storage, and an interface to the php.ini's 
session module settings from within the Session class. Session4_custom 
could not only store data in the CT_* containers (custom save handler), 
but also use native save handlers - files, mm and so on. Also, because 
of the incompatibilities of the new session4_custom with the old 
User class ideology, I introduced the new rewritten User class - User4,
which was no longer depend on Session, with compatible public API but 
very different guts. Prepend.php & page.inc were also changed a bit 
to reflect the session changes.
Then, Ulf Wendel came and said he need clean and working class
urgently. He began to hack session4_custom. He splet the
Session4_custom class in two parts, the first class with common session 
API (now in session4.inc), the second with the custom save handler 
(now in session4_custom.inc),which extended the first.

As a result, the Teodor's class had been acquired by the new Session4
class, the session4_custom was simplified, both were put to
php-lib/php/session CVS directory, and Barendt's phplib4 moved to unsup
directory.

So, there's two implementations of PHP4 native sessions in the PHPLib
CVS - session4.inc & session4_custom.inc in the php-lib/php/session
and phplib4 stuff (I see it in various places), which is not mine.


User Contributed Notes
PHP4 Sessions with PHPlib using session4_custom.inc*
add a note about notes
There are no user contributed notes for this page.


previousPHP4 Sessions with PHPlib using session4.inc*
Authenticationnext

Last updated: Thu, 04 Jul 2002



 

PHP Copyright © 2001 The PHPlib Group
All rights reserved.
Last updated: Thu Jul 4 15:34:59 2002 IST
Hosted and Maintained by http://www.sanisoft.com