Php uses a database to save the session

Source: Internet
Author: User
Php uses a database to save the session, read php uses a database to save the session, and php saves the session in the file mode by default, this can only be used on windows with a very low File space overhead. However, if we use a file system on uinx or liux, the file space overhead of such a file system is very large. "> <LINKhref =" h

By default, the php session is saved as a file, which can be used only on windows with a low File space overhead, however, if we use a file system on the uinx or liux, the file space overhead of such a file system is very large, but the session must be used all the time, A large number of users create a lot of session files, which brings performance problems to the entire server. on the other hand, if the server starts to use the cluster method, the session consistency cannot be maintained, so we are ready to use the database method to save the session, so that no matter how many servers are used at the same time, as long as their sessions are saved on a database server, they can save the complete session. continue with the implementation of the session.

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 database 'session '; create table 'session' (id CHAR (30) not null, 'user' CHAR (30), data CHAR (3000 ), parmiry by ('id '));
Compile php files
'Http: // www.knowsky.com

$ 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

?> Save as session_user_start.php.


Now our work has been completed, as long as you need to use session, set session_user_start.php. Include in. Note that this file must be included in the first line of the file, and you can use the same method as the session of the file.

This article is written in a rush. if there is anything wrong, please give your valuable comments. Welcome to your ax.
Http://blog.csdn.net/eoe2005/archive/2007/02/20/1512131.aspx

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.