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 solve the problem of too many Session files in PHP

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

Share

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

Editor to share with you how to solve the problem of too many Session files in PHP, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

What is the session file?

The file is usually / tmp/sessions/sess_4b1e384ad74619bd212e236e52a5a174If

Username | SRV 9: "test"; admin | Svv 1: "0"

2. When will session recovery occur

By default, there is a 100% probability of recycling for every php request, so it may be simply understood as "one for every 100 php requests." This probability is controlled by the following parameters

# the probability is gc_probability/gc_divisorsession.gc_probability = 1session.gc_divisor = 100,

Suppose this is the case, gc_maxlifetime=120. If the last modification time of a session file is 120 seconds, then the session is still valid until the next collection (the probability of 100%).

If your session uses the session,session recycling mechanism elsewhere in session.save_path, it may not automatically process out-of-date session files. At this point, you need to delete expired session manually (or crontab) regularly.

Set up the session storage directory

To use php5-fpm, modify / etc/php5/fpm/php.ini, modify or add the following line:

Session.save_path = 3600 TMP PUP

IV. Session cleanup script

#! / bin/shfind / tmp/php-session-cmin + 24-name "sess_*"-and-size 0-delete > / dev/null 2 > & 1find / tmp/php-session-cmin + 1440-name "sess_*"-delete > / dev/null 2 > & 1

The time here can be obtained through session.gc_maxlifetime and put into scheduled tasks (crontab)

Other ways

Use memcache, etc. (session.save_handler = memcache)

Use cookie, but cookie has to be encrypted

Use tmfs to store session

1. Mount / tmp as a tmpfs file system

Modify / etc/fstab by adding the following on the last line: / tmp/sessions tmpfs defaults,size=5120m 0 0

Mount-a

2. 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 is also easy to use, with the following contents

#! / bin/shdir= "0 1 2 3 4 5 6 7 8 9 a b c d e f" for levela in $dir;dofor levelb in $dir;dofor levelc in $dir;domkdir-p / tmp/sessions/$levela/$levelb/$levelc;donedone;donechown-R root:webgrp / tmp/sessions & & chmod-R 1777 / tmp/sessions

Because / tmp/sessions 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

3. Store session in different directories

Php itself supports multi-level hashing of session. In php.ini, change; session.save_path = / tmp to

Session.save_path = "3tmpplink"

4. Recovery of session

Just use the script above

Now let me tell you where the php session data is saved.

On the server side, of course, but not in memory, but in a file or database.

By default, the SESSION save method set in php.ini is files (session.save_handler = files), even if the SESSION data is saved by reading and writing files, while the directory saved by the SESSION file is specified by session.save_path, and the file name is prefixed with sess_, followed by SESSION ID, such as: sess_c72665af28a8b14c0fe11afe3b59b51b. The data in the file is the serialized SESSION data.

If you have a large number of visits, you may generate more SESSION files. At this time, you can set up a hierarchical directory to save SESSION files, and the efficiency will be improved a lot. The setting method is: session.save_path= "Numbababash saving path", N for the graded series, and save_path for the start directory.

When writing SESSION data, PHP will get the SESSION_ID of the client, and then according to this SESSION ID to the specified SESSION file storage directory to find the corresponding SESSION file. If it does not exist, it will be created. Finally, the data will be serialized and written to the file. Reading SESSION data is a similar operation flow. The read data need to be de-serialized to generate the corresponding SESSION variables.

The above is all the contents of this article entitled "how to solve the problem of too many Session files in PHP". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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