Mivec Framework comes with an utility class, SessionStorage, for consolidating all session variables in a single session variable, as defined in the configuration parameter “mivec.settings.session.variable-name”, which is defaulted to “mivec.session”. You are recommended to change this value, however, to a unique name to prevent possible conflicts.
To store a variable to the session, use SessionStorage::setAttribute($name, $value). Example:
SessionStorage::setAttribute("auth-token", "84656381");
To get a variable from the session, use SessionStorage::getAttribute($name). Example:
$authToken = SessionStorage::getAttribute("auth-token");
If the name is not found in the session, SessionStorage will return null.
At configuration parameter “mivec.settings.session.name”, we can change the name of the session as necessary, instead of using PHPSESSID which is PHP’s default.
SessionStorage allows plugging in custom session handlers, instead using PHP’s default session handler, which stores user’s session data at the server’s temporary directory.
The most common implementation of such session handler is using database as the storage backend. At Mivec Framework we provide implementations using native MySQL and SQLite calls.
To enable session handling with custom session handlers, we need to add the configuration parameter “mivec.settings.session.handler”, with the name of the custom session handler (i.e. MySQLSessionHandler and SQLiteSessionHandler).
You may also write your own session handler, if necessary, by extending SessionHandler:
require_once MIVEC_HOME . "/util/session-handler/SessionHandler.class.php"; class MySessionHandler extends SessionHandler { }
and implement the sessionOpen(), sessionClose(), sessionRead(), sessionWrite(), sessionDestroy() and sessionGC() methods. All of the above methods are required to be overrided, except sessionClose(), unless you have to use this method to close opened resources, such as database connections, file pointers, etc.
Then, add the configuration parameter “mivec.settings.session.handler.class” to tell SessionStorage where can it find the file for the specified custom session handler.
« Prev: Friendly URLs with Mivec Framework Next: Securing Web Applications Powered by Mivec Framework »