|
session_set_save_handlerSets user-level session storage functions Description
bool session_set_save_handler
( callable
$open
, callable $close
, callable $read
, callable $write
, callable $destroy
, callable $gc
)Since PHP 5.4 it is possible to register the following prototype:
bool session_set_save_handler
( SessionHandlerInterface
$sessionhandler
[, bool $register_shutdown = true
] )session_set_save_handler sets the user-level session storage functions which are used for storing and retrieving data associated with a session. This is most useful when a storage method other than those supplied by PHP sessions is preferred. i.e. Storing the session data in a local database. ParametersThis function has two prototypes.
Return Values
Returns Examples
Example #1 Custom session handler: see full code in SessionHandlerInterface synposis. The following code is for PHP version 5.4.0 and above. We just show the invokation here, the full example can be seen in the SessionHandlerInterface synposis linked above. 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 Example #2 Custom session save handler using objects The following code is for PHP versions less than 5.4.0.
The following example provides file based session storage similar to the
PHP sessions default save handler Note we additionally register the shutdown function session_write_close using register_shutdown_function under PHP less than 5.4.0. This is generally advised when registering objects as session save handlers under PHP less than 5.4.0.
<?php NotesWarning
When using objects as session save handlers, it is important to register the
shutdown function with PHP to avoid unexpected side-effects from the way
PHP internally destroys objects on shutdown and may prevent the
As of PHP 5.4.0 you can use session_register_shutdown or simply use the 'register shutdown' flag when invoking session_set_save_handler using the OOP method and passing an instance that implements SessionHandlerInterface. Warning
As of PHP 5.0.5 the It is possible to call session_write_close from the destructor to solve this chicken and egg problem but the most reliable way is to register the shutdown function as described above. Warning
Current working directory is changed with some SAPIs if session is closed in the script termination. It is possible to close the session earlier with session_write_close. Changelog
See Also
|