Explore how to store sessions in the database

Source: Internet
Author: User

By default, php sessions are stored in the file format. you can see this row in ini, session. save_handler = "files", which means to use files to save sessions. To use databases to store sessions, we need to change it to the Support Mode and rename it session. save_handler = "use". However, this only means that we have not stored the session in the file mode. We also need to select a database and create a database table.

To create a table structure for databases and databases, we can use any database that php can use. Because php and mysql have the best combination, I will use mysql as an example, of course, you can change the name of another database based on your needs. At the same time, because mysql has no transaction function, it is faster than other databases. However, you can save the session book and never process things, in addition, I decided better.
Create a database:
Copy codeThe Code is as follows: create database 'session '; create table 'session' (id CHAR (30) not null, 'user' CHAR (30), data CHAR (3000 ), parmiry by ('id '));

Next we will compile the session-saving file session_start.php.Copy codeThe Code is as follows: <? Php
$ Con = mysql_connection ("127.0.0.1", "user", "pass ");
Mysql_select_db ("session ");
Function open ($ save_path, $ session_name)
{
Return (true );
}
Function close ()
{
Return (true );
}
Function read ($ id)
{
If ($ result = mysql_query ("SELECT * FROM session WHERE id = '$ id '"))
{
If ($ row = mysql_felth_row ($ result ))
{Return $ row ["data"];}
}
Else
{
Return "";
}
}
Function write ($ id, $ sess_data)
{
If ($ result = mysql_query ("UPDATE session SET data = '$ sess_data' WHERE id = '$ id '"))
{
Return true;
}
Else
{
Return false;
}
}
Function destroy ($ id)
{
If ($ result = mysql_query ("DELETE * FROM session WHERE id = '$ id '"))
{
Return true;
}
Else
{
Return false;
}
}
/*************************************** ******
* WARNING-You will need to implement some *
* Sort of garbage collection routine here .*
**************************************** *****/
Function gc ($ maxlifetime)
{
Return true;
}
Session_set_save_handler ("open", "close", "read", "write", "destroy", "gc ");
Session_start ();
// Proceed to use sessions normally
?>

Now our work has been completed, as long as you need to use session, set session_user_start.php. Included,
Note:, This file must be included in the first line of the file, and then you want to use the same method as the session of the file.

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.