php 会话的默认生命周期是多少?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/156712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the default lifetime of a session?
提问by Greg B
If I hit a page which calls session_start(), how long would I have to wait before I get a new session ID when I refresh the page?
如果我点击了一个调用 的页面,session_start()刷新页面时我需要等待多长时间才能获得新的会话 ID?
采纳答案by Martin
Check out php.ini the value set for session.gc_maxlifetime is the ID lifetime in seconds.
查看 php.ini 为 session.gc_maxlifetime 设置的值是以秒为单位的 ID 生存期。
I believe the default is 1440 seconds (24 mins)
我相信默认值是 1440 秒(24 分钟)
http://www.php.net/manual/en/session.configuration.php
http://www.php.net/manual/en/session.configuration.php
Edit:As some comments point out, the above is not entirely accurate. A wonderful explanation of why, and how to implement session lifetimes is available here:
编辑:正如一些评论指出的那样,上述内容并不完全准确。此处提供了有关为什么以及如何实现会话生存期的精彩解释:
回答by flamingLogos
The default in the php.ini for the session.gc_maxlifetimedirective (the "gc" is for garbage collection) is 1440 seconds or 24 minutes. See the Session Runtime Configuation page in the manual:
session.gc_maxlifetime指令的 php.ini 中的默认值(“gc”用于垃圾收集)是 1440 秒或 24 分钟。请参阅手册中的会话运行时配置页面:
http://www.php.net/manual/en/session.configuration.php
http://www.php.net/manual/en/session.configuration.php
You can change this constant in the php.ini or .httpd.conf files if you have access to them, or in the local .htaccess file on your web site. To set the timeout to one hour using the .htaccess method, add this line to the .htaccess file in the root directory of the site:
如果您可以访问它们,您可以在 php.ini 或 .httpd.conf 文件中或在您网站上的本地 .htaccess 文件中更改此常量。要使用 .htaccess 方法将超时设置为一小时,请将此行添加到站点根目录中的 .htaccess 文件中:
php_value session.gc_maxlifetime "3600"
Be careful if you are on a shared host or if you host more than one site where you have not changed the default. The default session location is the /tmp directory, and the garbage collection routine will run every 24 minutes for these other sites (and wipe out your sessions in the process, regardless of how long they shouldbe kept). See the noteon the manual page or this sitefor a better explanation.
如果您使用的是共享主机,或者您托管了多个未更改默认值的站点,请务必小心。默认会话位置是 /tmp 目录,垃圾收集例程将为这些其他站点每 24 分钟运行一次(并在此过程中清除您的会话,无论它们应该保留多长时间)。见注手册页面或本网站的一个更好的解释。
The answer to this is to move your sessions to another directory using session.save_path. This also helps prevent bad guys from hiHymaning your visitors' sessions from the default /tmp directory.
答案是使用 session.save_path 将您的会话移动到另一个目录。这也有助于防止坏人从默认的 /tmp 目录劫持访问者的会话。
回答by jochil
it depends on your php settings...
use phpinfo()and take a look at the session chapter. There are values like session.gc_maxlifetimeand session.cache_expireand session.cookie_lifetimewhich affects the sessions lifetime
这取决于您的 php 设置...
使用phpinfo()并查看会话章节。有像session.gc_maxlifetimeand 和session.cache_expireand 之类的值session.cookie_lifetime会影响会话的生命周期
EDIT: it's like Martin write before
编辑:就像马丁之前写的
回答by Junior Mayhé
According to a user on PHP.netsite, his efforts to keep session alive failed, so he had to make a workaround.
据PHP.net站点上的一位用户称,他保持会话活动的努力失败了,因此他不得不采取一种解决方法。
<?php
$Lifetime = 3600;
$separator = (strstr(strtoupper(substr(PHP_OS, 0, 3)), "WIN")) ? "\" : "/";
$DirectoryPath = dirname(__FILE__) . "{$separator}SessionData";
//in Wamp for Windows the result for $DirectoryPath
//would be C:\wamp\www\your_site\SessionData
is_dir($DirectoryPath) or mkdir($DirectoryPath, 0777);
if (ini_get("session.use_trans_sid") == true) {
ini_set("url_rewriter.tags", "");
ini_set("session.use_trans_sid", false);
}
ini_set("session.gc_maxlifetime", $Lifetime);
ini_set("session.gc_divisor", "1");
ini_set("session.gc_probability", "1");
ini_set("session.cookie_lifetime", "0");
ini_set("session.save_path", $DirectoryPath);
session_start();
?>
In SessionData folder it will be stored text files for holding session information, each file would be have a name similar to "sess_a_big_hash_here".
在 SessionData 文件夹中,它将存储用于保存会话信息的文本文件,每个文件的名称类似于“sess_a_big_hash_here”。
回答by Sliq
But watch out, on most xampp/ampp/...-setups and some linux destributions it's 0, which means the file will never get deleted until you do it within your script (or dirty via shell)
但请注意,在大多数 xampp/ampp/...-setups 和一些 linux 发行版中,它是 0,这意味着该文件永远不会被删除,除非您在脚本中执行它(或通过 shell 弄脏)
PHP.INI:
PHP.INI:
; Lifetime in seconds of cookie or, if 0, until browser is restarted.
; http://php.net/session.cookie-lifetime
session.cookie_lifetime = 0
回答by Eduardo Cuomo
You can use something like ini_set('session.gc_maxlifetime', 28800); // 8 * 60 * 60too.
你也可以使用类似的东西ini_set('session.gc_maxlifetime', 28800); // 8 * 60 * 60。

