Session HandlingTable of Contents
The SessionHandler classIntroduction
SessionHandler a special class that can be used to expose
the current internal PHP session save handler by inheritance. There are six methods which
wrap the six internal session save handler callbacks ( When a plain instance of SessionHandler is set as the save handler using session_set_save_handler it will wrap the current save handlers. A class extending from SessionHandler allows you to override the methods or intercept or filter them by calls the parent class methods which ultimately wrap the interal PHP session handlers.
This allows you, for example, to intercept the Because the SessionHandler wraps the current internal save handler methods, the above example of encryption can be applied to any internal save handler without having to know the internals of the handlers. To use this class, first set the save handler you wish to expose using session.save_handler and then pass an instance of SessionHandler or one extending it to session_set_save_handler. Please note the callback methods of this class are designed to be called internally by PHP and are not meant to be called from user-space code. The return values are equally processed internally by PHP. For more information on the session workflow, please refer session_set_save_handler. Class synopsisSessionHandler
class SessionHandler
implements
SessionHandlerInterface
{
/* Methods */
public bool close
( void
)
public bool destroy
( string
$session_id
)
public bool gc
( int
$maxlifetime
)
public bool open
( string
$save_path
, string $session_id
)
public string read
( string
$session_id
)
public bool write
( string
}$session_id
, string $session_data
)Warning
This class is designed to expose the current internal PHP session save handler, if you you want to write your own custom save handlers, please implement the SessionHandlerInterface interface instead of extending from SessionHandler. Example #1 Using SessionHandler to add encryption to internal PHP save handlers.
<?php
The SessionHandlerInterface classIntroductionSessionHandlerInterface is an interface which defines a prototype for creating a custom session handler. In order to pass a custom session handler to session_set_save_handler using its OOP invocation, the class must implement this interface. Please note the callback methods of this class are designed to be called internally by PHP and are not meant to be called from user-space code. Class synopsisSessionHandlerInterface
class SessionHandlerInterface
{
/* Methods */
abstract public bool close
( void
)
abstract public bool destroy
( string
$session_id
)
abstract public bool gc
( string
$maxlifetime
)
abstract public bool open
( string
$save_path
, string $name
)
abstract public string read
( string
$session_id
)
abstract public bool write
( string
}$session_id
, string $session_data
)Example #1 Example using SessionHandlerInterface
The following example provides file based session storage similar to the
PHP sessions default save handler Note we use the OOP prototype with session_set_save_handler and register the shutdown function using the function's parameter flag. This is generally advised when registering objects as session save handlers.
<?php |