After demonstrating how the “session_set_save_handler()” can be used to create a personalized session handling mechanism, I’ll go one step further by creating a new set of callback functions, which uses a MySQL database table for managing sessions. First, the definition for the corresponding callback functions is listed below: // define 'openSession()' function As you can see, in the above example I defined the six callback functions that will be passed as arguments to the “session_set_save_handler(),” in such a way that they use a simple MySQL database table in order to read and write session data. This database table stores session IDs, the serialized session data, and finally a timestamp, which can be useful for assigning a time expiration to a particular session, so it can be deleted from the mentioned database table when the PHP garbage collection mechanism is appropriately triggered. (Notice that this situation may vary from system to system, in accordance with some settings of the session module within the php.ini file, such as the “session.gc_maxlifetime” and “session.gc_probability” directives). Also, notice that most of the callback functions use a global $db variable, in order to have access to an instance of a MySQL wrapping class, which is utilized to connect to MySQL, together with performing all the SQL queries against the respective database table. Also, a MySQL result set processing class is used internally, in this case represented by the $result variable. Regarding these classes, in the next section I’ll show you their corresponding definition, thus you can have at hand all the source code required for implementing the previous MySQL-based session mechanism. Keep on reading to learn more.
blog comments powered by Disqus |