In this section, we are going to learn how we can extend the session handling mechanism of PHP to use a MongoDB database for managing sessions instead of using the filesystem. Before we delve into implementation, we are going to briefly cover the basics, mainly the session_set_save_handler()
function.
The session_set_save_handler()
function allows us to define our own functions for storing and retrieving session data. The function takes six arguments, each one being the name of a callback function. This is what the method signature looks like:
bool session_set_save_handler(callback $open, callback $close, callback $read, callback $write, callback $destroy, callback $gc)
Let's see what each of these callback functions do:
open():
This method is called whenever a session is initiated withsession_start()
. It takes two arguments, the path to where the session will be stored and the name of the...