Start a PHP session using MySQL

Source: Internet
Author: User
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:

  1. 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.
  2. It is difficult for a distributed website to share sessions using session files.
  3. 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:

  1. DROP TABLE IF EXISTS `Sessions`;
  2. CREATE TABLE `Sessions` (
  3. `Session_id` Varchar(32) NOT NULL Default '',
  4. `User_id` Int(10) Unsigned

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.