How to store a session in a database

Source: Internet
Author: User
This article mainly introduces about how to store the session in the database, has a certain reference value, now share to everyone, the need for friends can refer to

How to store the session in a database can be combined with a data table design description.

By default php.ini Session.save_handler = files, which is the session is stored as a file.

If you want to change to a database or other storage mode, you need to change the settings so that Session.save_handler = user.

In addition to being configured in php.ini, you can configure it separately in the PHP page, using

Ini_set (' Session.save_handler, ' user ') to set the session storage mode, set to user-defined storage.

After you have set up the storage method, you need to use the Session_set_save_handler () function.

This function is a function that sets the session save procedure at the user level. The function has 6 parameters, these 6 parameters are actually the names of 6 custom functions, which represent the opening, closing, reading, writing, destroying, GC (garbage Collection) of the session respectively.

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 the session as usual.

The database structure is as follows:

session_id, Session_value, Expire_time, stores the ID and value of the SessionID, respectively, and the expiration time.

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.