Detailed explanation of several methods to improve the response speed of SESSION by PHP

  • 2021-12-19 06:01:04
  • OfStack

In this paper, several methods to improve the response speed of SESSION by PHP are described with examples. Share it for your reference, as follows:

Setting up multilevel directory storage SESSION

The default storage directory of session is Level 1 directory. If the number of users is large, the number of session files is relatively large. We can set the number of directories to 2, and use Level 2 directory to submit the search and access speed. However, this method is not obvious to improve the speed. You can modify php. ini, and then modify the number of session storage directories.


session.save_path = "2;/tmp"

Store SESSION in redis

session in php is stored in a file by default, and supports redis storage mode, because the key value data of redis is stored in memory, which can improve the access speed of session.


session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379"

Release the lock of SESSION file in time

When we use session, we need to execute the session_start() Function.

session_start() The function works as follows:

It is determined whether the http request contains an cookie named PHPSESSID, and if not, the cookie is created and written to the header file of the http response.

Find the corresponding session file through PHPSESSID, open the file by reading and writing, and then read the data inside to memory.

Then we will read or set the value of session through the super-global variable $_ SESSION. When we operate, the value of session is saved in memory, and it will not be written to the corresponding file until the page is executed by default.

We tested this process with the following 1 piece of code:

Analysis of session Execution Process


include "session_function.php"; 
//session_function.php The code is in the appendix 
session_start();
$_SESSION['name']="koastal";
echo "<br/>html content<br/>";
var_dump($_SESSION);
echo "<br/>";

Output result

open
read
html content
array (size=1)
'name' = > string 'koastal' (length=7)
shutdown
write
close

From the above example, it can be found that the session file is locked during the page execution process (the page execution process refers to the time spent parsing the php file into the corresponding html file, not the user's residence time on the page).

For some time-consuming operations, such as uploading files and sending emails, if the user uploads files in the page upload. php, and then he opens the personal information page ES90php, but the upload. php file has not been completed, session_start () in index. php cannot open the corresponding session file.

session Deadlock Sample Program

upload.php


<?php
session_start();
$_SESSION['name']='koastal';
sleep(10); //sleep 10s  Simulated file upload time consuming 

index.php


<?php
session_start();
var_dump($_SESSION);

From the above analysis, we can know that session will not write to the file until the page is executed, and release the file lock. The function of session, we are generally used in part 1 of the page. When we finish processing session, we can use session_commit () or session_write_close () function to write the value of session to the file in advance and release the file lock.

Analysis of session_commit Process


include "session_function.php"; 
//session_function.php The code is in the appendix 
session_start();
$_SESSION['name']="koastal";
session_commit();
echo "<br/>html content<br/>";
var_dump($_SESSION);
echo "<br/>";

Output result

open
read
write
close

html content
array (size=1)
'name' = > string 'koastal' (length=7)

shutdown

As we can see, it is being implemented session_commit() After that, the session contents are written to the file in advance, and the file is closed (the file lock is released).

Deadlock example optimized program

upload.php


<?php
session_start();
$_SESSION['name']='koastal';
session_commit();
sleep(10); //sleep 10s  Simulated file upload time consuming 

After we operate session, we first talk about writing session data to a file, and then execute the upload process, so that it will not affect other pages to use the session file.

After executing session_commit


<?php
session_start();
$_SESSION['name'] = "koastal";
session_commit();
echo $_SESSION['name'];

The other name of session_commit is session_write_close, which means writing session information and closing the file. After session_commit, although the session file is closed, the page is still executing. As long as session_unset is not executed, the value of session is still saved in memory, so we can also obtain and output the value of session. In fact, we can also assign a value to session at this time, but this assignment is only assigned to memory, because the file has been closed, so other pages cannot access session_commit (); The value of session set after.

Destroy session

Now that we have discussed so many issues with session, we are talking about the operation of destroying session when the user logs out.

From the above analysis, we know that the value of session will be saved in memory and file.
session_start() The corresponding file is opened and session data is read into memory.
session_commit() session data in memory is written to a file and the file is closed.
Do not display calls session_commit() Will be called automatically after the page is executed.

Therefore, we need to delete the session information in memory and delete the session file.


session_unset();  // Delete the in-memory session Information 
session_destory(); // Delete session Documents 

What if we just use session_unset(); It can also achieve the effect of logging off users, but in this way, the data of session is set to be empty and written into session file, and the session file is not deleted in essence.

The above two operations destroy the session value on the server. In addition, we need to clean up the cookie saved in the client to complete the real logout operation.

Logout login complete code

logout.php


<?php
session_unset();  // Delete the in-memory session Information 
session_destory(); // Delete session Documents 
setcookie('PHPSESSID', null, time()-10);// Will cookie Value to expire 

Appendix

session_function.php


session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379"

0

For more readers interested in PHP related content, please check the topics on this site: "Summary of php Cache Technology", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage", "Summary of PHP Error and Exception Handling Methods", "Introduction to php Object-Oriented Programming", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: