SESSION PHP methods to improve the response speed of which

1, set up multi-level directory to store SESSION

The default session storage directory is the directory level 1, if the user is larger than the number of session file is relatively large, we can set the directory number 2, level 2 directory lookup can submit and access speed. However, this approach to enhance the speed is generally not very obvious, you can modify the php.ini, and then modify the number of session storage directory.

1

session.save_path = "2;/tmp"

2, the SESSION stored in redis

php The session is stored in the default file, redis storage support, because redis key data stored in the memory, the session can increase access speed.

1

2

session.save_handler = redis

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

3, the timely release file lock SESSION

When we use the session, you need to perform the session_start()function.

session_start () function returns the following:

Http determine whether the request contains cookie called PHPSESSID, if not the cookie is created and written to the header http response.

Look through the corresponding session PHPSESSID file, a file opened for reading and writing, and then read the data inside the memory.

Then we will generally pass the $ _SESSION super-global variables, read or set the value of the session, when we operate, the value of session are stored in memory by default after the page is finished, will be written to the corresponding file.

We tested this process with the following piece of code:

SESSION implementation process analysis:

1

2

3

4

5

6

7

include "session_function.php";

//session_function.php代码在附录

session_start();

$_SESSION['name']="koastal";

echo "<br/>html content<br/>";

var_dump($_SESSION);

echo "<br/>";

Output:

1

2

3

4

5

6

7

8

open

read

html content

array (size=1)

  'name' => string 'koastal' (length=7)

shutdown

write

close

It can be found by way of example above, during execution of the page (page execution file parsing php refers to as an html file corresponding to the time-consuming, rather than the residence time of the user on the page), the session file is locked of.

Guess you like

Origin www.cnblogs.com/heyue0117/p/11827807.html