Detailed explanation of php Session invalid analysis data collection

黄舟
Release: 2023-03-06 07:20:01
Original
906 people have browsed it

php Session invalid analysis

During the PHP development process, some friends may often encounter the problem that the files generated by the Session cannot be automatically cleared. In fact, it is not really impossible to clear, but There is a probability problem. As long as your site has a large enough traffic, those files can be automatically cleared. If the number of visits is relatively small and the files are not pleasing to the eye, you can realize the automatic clearing function of Session files by configuring it in php.ini. The specific configuration is as follows:

Find

session.gc_probability = 1
session.gc_pisor = 1000
Copy after login

The above two parameters are actually this probability, which is 1/1000 by default
Change session.gc_pisor = 1000 to session.gc_pisor = 100
If you want to achieve complete real-time, then You can change this parameter to 1, so that the probability is 100%
See how session works
Overview: Every PHP request has a probability of 1/100 (default value) Trigger "session recycling". If "session recycling" occurs, the /tmp/sess_* files will be checked. If the last modification time exceeds 1440 seconds (the value of gc_maxlifetime), they will be deleted, which means that these sessions have expired.
1. How does session exist on the server side (usually Apache with PHP module)?
By default, PHP will save the session in the /tmp directory, and the file name is like this: sess_01aab840166fd1dc253e3b4a3f0b8381. Each file corresponds to a session.

more /tmp/sess_01aab840166fd1dc253e3b4a3f0b8381
username|s:9:”jiangfeng”;admin|s:1:”0〃;
Copy after login


#Variable name|Type: length: value

Deleting the session file here means that the corresponding session is invalid.

2. How does session exist on the client side (usually the browser)?

session On the browser side, you only need to save the session ID (the unique ID generated by the server side). There are two ways to save it: in cookies and in URLs. If the session ID is saved in the cookie, you can see that there is a PHPSESID variable in the browser's cookie. If it is passed by URL, you can see the URL in the form:

index.php?PHPSESID=01aab840166fd1dc253e3b4a3f0b8381. (On the server side, use session.use_cookies to control which method is used)

3. On the server side, how does PHP determine whether the session file has expired?

If the "last modification time" to "now" exceeds gc_maxlifetime (default is 1440) seconds, this session file is considered expired. When the next session is recycled, if this file If it still has not been changed, the session file will be deleted (the session will expire).

Simply put, if I log in to a website and there is no operation within 1440 seconds (default value), then the corresponding session is considered to have expired.

So, modifying the gc_maxlifetime variable in the php.ini file can extend the session expiration time: (for example, we modify the expiration time to 86400 seconds)

session.gc_maxlifetime = 86400
Copy after login

Then, just restart your web service (usually apache).

Note: PHP5 uses a recycling mechanism when session expires. The time set here is 86400 seconds. If the session has not been modified within 86400 seconds, it will not be deleted until the next "recycling".

4. When does session "recycling" occur?

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

#概率是gc_probability/gc_pisor
session.gc_probability = 1
session.gc_pisor = 100
Copy after login

Note 1: Assume that in this case gc_maxlifetime=120, if a session file was last modified 120 seconds ago, then in the next recycling ( 1/100 probability) occurs, this session is still valid.

Note 2: If your session uses session.save_path to save the session elsewhere, the session recycling mechanism may not automatically process expired session files. At this time, you need to delete expired sessions manually (or crontab) regularly:

cd /path/to/sessions; find -cmin +24 | xargs rm
Copy after login

5. Some special circumstances

Because the recycling mechanism will check The "last modification time" of the file, so if a session is active but the content of the session has not changed, then the corresponding session file has not changed, and the recycling mechanism will consider this to be a session that has not been active for a long time. Delete it. This is something we don't want to see. We can solve this problem by adding the following simple code:

<?php 
if(!isset($_SESSION[&#39;last_access&#39;])||(time()-$_SESSION[&#39;last_access&#39;])>60)
  $_SESSION[&#39;last_access&#39;] = time(); 
?>
Copy after login

The code will try to modify the session every 60 seconds.

Summary: If you want to modify the session expiration time, just modify the variable gc_maxlifetime. PHP5's session uses a passive recycling mechanism (garbage collection). Expired session files will not disappear by themselves, but expired sessions will be processed by triggering "recycling".

Let’s take a detailed look at some other issues of setting session time

Session expiration time parameters

Set the expiration time parameters, mainly Just set the parameters of session.gc_maxlifetime. For a safer setting, just set the following two parameters.

ini_set(&#39;session.cookie_lifetime&#39;, 0); // 可用 print_r(session_get_cookie_params()); 观察
ini_set(&#39;session.gc_maxlifetime&#39;, 3600); // 可用 echo ini_get("session.gc_maxlifetime"); 观察
Copy after login

session_cookie_lifetime 设为 0 的话, 代表等到 browser 才把此 cookie 清掉.(session 和 browser cookie 是有相关的)

如果懒得想这些, 直接用下面的 function 就可以了
Session 过期时间程式

<?php
function start_session($expire = 0)
{
  if ($expire == 0) {
    $expire = ini_get(&#39;session.gc_maxlifetime&#39;);
  } else {
    ini_set(&#39;session.gc_maxlifetime&#39;, $expire);
  }
  if (empty($_COOKIE[&#39;PHPSESSID&#39;])) {
    session_set_cookie_params($expire);
    session_start();
  } else {
    session_start();
    setcookie(&#39;PHPSESSID&#39;, session_id(), time() + $expire);
  }
}
?>
Copy after login

使用方式

于程式最上方加入: start_session(600); // 代表 600 秒后会过期 (取代原本 session_start())
如果要再延长过期时间, 只要再做修改即可.
但是有个问题要注意, 就是 PHP 的 session 预设是存成 file, 所以 /tmp 可能会因这样设定而爆掉(档案太多), 通常解法是把 session 存进 DB/memcache 中.

 以上就是php Session无效分析资料整理的详解的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!