By default, PHP sessions (session) are saved by a file. There are several disadvantages to doing this:
Session files are generally small, but there are a lot of files, and saving many of these small files in a file system is a waste of space and inefficient.
Distributed sites have difficulty using session files to share sessions.
Session file mode is not conducive to the statistics of online users of the session information.
To solve the above problems, we can consider using a database to save session information.
For PHP development, saving sessions with MySQL is a very good choice. MySQL provides an in-memory table type HEAP, which can be considered for further optimization of performance if the amount of data per session is small. However, a table with a Heap type has many limitations, such as that it does not support fields of type text, so it is appropriate to choose MyISAM if the length of the session data is not predictable, and this type of table has no processing overhead and can achieve optimal performance for disk-based tables.
The following is the structure of the sessions table:
DROP TABLE IF EXISTS `sessions`;
CREATE TABLE `sessions` (
`session_id` varchar(32) NOT NULL default '',
`user_id` int(10) unsigned NOT NULL default '0',
`data_value` text NOT NULL,
`last_visit` timestamp(14) NOT NULL,
PRIMARY KEY (`session_id`),
KEY `user_id` (`user_id`)
) TYPE=MyISAM;
PHP supports the user session module, which allows you to set up custom session-handling functions by Session_set_save_handler. Because the default processing module is files, you should use Session_module_name (' user ') to tell PHP to use the user session module before setting the session handler function with Session_set_save_handler, and Session_ Set_save_handler must be executed before session_start.