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.