By default, PHP sessions are retained through files. This solution has the following problems: the session files are usually very small, but there are many files. it is a waste of space to keep many such small files in the file system, and the efficiency is not high.
By default, PHP sessions are retained through files. The following problems occur:
- Session files are usually very small, but there are many files. it is a waste of space to keep many such small files in the file system, and the efficiency is not high.
- It is difficult for a distributed website to share sessions using session files.
- The session file method is not conducive to collecting the session information of online users.
To solve the preceding headers, we can consider using databases to retain session information.
For PHP development, using MySQL for session persistence is a very good choice. MySQL provides a table type Heap established in the memory. if the data volume of each session is small, you can use this type of table to further optimize the performance. However, a Heap table has many restrictions. for example, it does not support text fields. Therefore, if you cannot guess the length of session data records, it is appropriate to select MyISAM, this type of table has no transaction processing overhead, and provides optimal performance for disk-based tables.
The structure of the sessions table is as follows:
- DROP TABLE IF EXISTS `Sessions`;
- CREATE TABLE `Sessions` (
- `Session_id` Varchar(32) NOT NULL Default '',
- `User_id` Int(10) Unsigned