By default, php retains sessions by using the file method, 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 high. However, session must be applied all the time.
By default, php retains sessions by using the file method, 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 high. However, session must be applied all the time, A large number of users create a large number of session files, which brings performance titles to all servers. on the other hand, if the server starts to use the cluster method, the session consistency cannot be maintained, so we should stop using the database method to retain the session, so that no matter how many servers are applied at the same time, as long as their sessions are retained on a database server, the session integrity can be retained. For more information about how to implement the session, see.
Php sessions are retained by default by the file method. you can see this row in ini, session. save_handler = 'Files'. This means that the session is retained by the file. if you want to retain the session by using the database, you need to change it to the "wearing" mode and rename it the session. save_handler = 'use' is enough. However, it only states that we do not use the file method to store Sessions. 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 apply. because php and mysql are the best in combination, I will use mysql as an example, of course, you can change the name of another database according to your needs. at the same time, because mysql has no effect on things, it is faster than other databases. However, you can keep the session book and never want to handle 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
$ Con = mysql_connection ('192. 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', 'deststroy', 'gc ');
Session_start ();
// Proceed to use sessions normally
?>
Keep it as session_user_start.php.
Now our work has been completed, as long as you need to apply session, set session_user_start.php. Please note that this file must be included in the first line of the file, and then you can apply the same method as the session of the file.
The promotion in this article, if there is something wrong, please put forward valuable ideas. Welcome to your ax.