So I have created a class implementing SessionhandlerInterface and set it as a session handler:
$sessionHandler = new SessionHandler();
session_set_save_handler($sessionHandler);
session_start();
The problem is, the write function is never called.
If I use the second parameter of session_set_save_handler and set it to false:
session_set_save_handler($sessionHandler, false);
Then it works properly. Can somebody explain this behaviour to me? I am using PHP 5.4.6.
In the documentation there is written:
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 write and close from being called. Typically you should register ‘session_write_close’ using the register_shutdown_function() function.
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.
But I don’t fully understand that.
The documentation states the following:
This is probably why you don’t see the function being called if you use a simple
echo.