Start a PHP session with MySQL

Source: Internet
Author: User
Tags file system php session sessions

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.



Related Article

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.