Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create session Storage folder and Recycle session by PHP

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "PHP how to create session storage folders and recycle session", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "PHP how to create session storage folders and recycle session"!

Session, in computers, especially in network applications, is called a "session".

The traffic of a server is relatively large, because of the need of the program, the expiration time of session is set to 3 hours, resulting in the accumulation of nearly 200000 session files under / tmp. This in turn leads to a sharp increase in the cpu occupied by the kernel. Because the reading and writing of session involves the random reading and writing of a large number of small files, and is concentrated in one directory, iowait also increases sharply.

First consider putting session in memory

The easiest way is to mount / tmp as a tmpfs file system, that is, in memory

The second step is to store the session in an impassable directory

Php itself supports multi-level hashing of session

In php.ini, set the

Session.save_path = / tmp

Change to

Session.save_path = "2ntplink tmphand session"

Means to store session in the / tmp/session folder with 2 and a hash.

Save exit and restart php after the end of step 3

Step 3, create a session storage folder

Php does not automatically create these folders, but some scripts to create them are provided in the source file. The following script also works.

I = "0 1 2 3 4 5 6 7 8 9 a b c d e f" for acm in $I; do for x in $I; do mkdir-p / tmp/session/$acm/$x; done; done chown-R nobody:nobody / tmp/session chmod-R 1777 / tmp/session

Because / tmp uses memory, all files in it will be lost after the server is restarted, so you need to add the above script to / etc/rc.local and put it before starting php

The fourth step is the recovery of session

Session will expire after session.gc_maxlifetime, but it will not be deleted immediately. After a long time, it will take up a lot of / tmp space. The specific deletion algorithm is too lazy to study. The following command removes expired session. The expiration time I define here is 3 hours.

Find / tmp/session-amin + 180-exec rm-rf {}\

Put it into the cron, execute it every 10 minutes, and finish.

At this point, I believe you have a deeper understanding of "PHP how to create session storage folders and recycle session". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report