Home > Article > Backend Development > How to store SESSION in the database
This article mainly introduces how to store SESSION in the database, which has certain reference value. Now I share it with you. Friends in need can refer to
How to store SESSION in the database. It can be combined with the data table design instructions.
By default, session.save_handler = files in php.ini, that is, the session is stored in the form of a file.
If you want to change to a database or other storage method, you need to change the settings so that session.save_handler = user.
In addition to configuring in php.ini, you can also configure it separately in the PHP page. Use
ini_set ('session.save_handler, 'user') to set the session storage method. Customize storage methods for users.
After setting the storage method, you need to use the session_set_save_handler() function.
This function is a function that sets the user-level session saving process. This function has 6 parameters. These 6 parameters are actually the names of 6 custom functions, which respectively represent the opening, closing, reading, writing, destruction, and gc (garbage collection) of the session.
The sample code is as follows:
function open () { } function close() { } function read () { } function write () {} function destroy () {} function gc () {} session_set_save_handler ("open", "close", "read", "write", "destroy", "gc"); session_start();
Now you can use session as usual.
The database structure is as follows:
Session_id, session_value, expire_time, respectively store the id and value of sessionid and the expiration time.
Related recommendations:
php
Detailed explanation of how to set up a session that strictly controls the expiration time
Take you to understand the difference and usage of session and cookies (picture and text tutorial)
The above is the detailed content of How to store SESSION in the database. For more information, please follow other related articles on the PHP Chinese website!