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 need to create a lot of session files, which brings performance problems to the entire server.
On the other hand, if the server starts to adopt the cluster mode, the session consistency cannot be maintained. Therefore, we are ready to use the database mode to save the session. In this way, 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 stores session files. We use PHP to prepare the PHP file. 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 them to the user mode and rename them as sessions. 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, it does not need to be processed to store sessions. I think it is better here.
Create database 'session '; create table 'session' (id char (30) not null, 'user' char (30), data char (3000 ), parmiry by ('id '));
PHP saves session and writes PHP files
- < ?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;
- }
- }
- 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, we have completed saving the session in PHP, as long as you need to use the session, set session_user_start.PHP. This file must be included in the first line of the file, and can be used in the same way as the session of the file.