Custom Session Handlers
To implement database storage, or any other storage method, you will need to use session_set_save_handler() to create a set of user-level storage functions. As of PHP 5.4.0 you may create session handlers using the SessionHandlerInterface or extend internal PHP handlers by inheriting from SessionHandler.
The callbacks specified in session_set_save_handler() are methods
called by PHP during the life-cycle of a session: open
, read
,
write
and close
and for the housekeeping tasks:
destroy
for deleting a session and gc
for periodic garbage
collection.
Therefore, PHP always requires session save handlers. The default is usually the
internal 'files' save handler. A custom save handler can be set using
session_set_save_handler(). Alternative internal save handlers are also
provided by PHP extensions, such as sqlite
,
memcache
and memcached
and can be set with
session.save_handler.
When the session starts, PHP will internally call the open
handler followed by the
read
callback which should return an encoded string exactly as it was originally
passed for storage. Once the read
callback returns the encoded string, PHP will
decode it and then populate the resulting array into the $_SESSION superglobal.
When PHP shuts down (or when session_write_close() is called),
PHP will internally encode the $_SESSION superglobal and pass this
along with the session ID to the write
callback.
After the write
callback has finished, PHP will internally invoke the
close
callback handler.
When a session is specifically destroyed, PHP will call the destroy
handler with
the session ID.
PHP will call the gc
callback from time to time to
expire any session records according to the set max lifetime of a session.
This routine should delete all records from persistent storage which were
last accessed longer than the $lifetime
.
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-session.customhandler.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.