php 在 htaccess 中声明会话最大生命周期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6253498/
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
Declaring Session Max Life Time in htaccess
提问by Tarik
I am wondering whether we can declare session.gc_maxlifetime
setting in .htaccess
for the one particular project instead of whole web server?
我想知道我们是否可以为一个特定项目而不是整个 Web 服务器声明session.gc_maxlifetime
设置.htaccess
?
If so, how can we do that? Like the following code?
如果是这样,我们该怎么做?喜欢下面的代码吗?
php_value session.gc_maxlifetime 2000
php_value session.gc_maxlifetime 2000
I've tried it and it didn't work and also I created a php.ini
file in the same directory of my project and it didn't work either.
我试过了,它没有用,而且我在php.ini
我的项目的同一目录中创建了一个文件,它也不起作用。
Thanks.
谢谢。
回答by netcoder
Yes, session.gc_maxlifetime is a PHP_INI_ALL setting so it can be overidden in .htaccess:
是的,session.gc_maxlifetime 是一个 PHP_INI_ALL 设置,所以它可以在 .htaccess 中被覆盖:
php_value session.gc_maxlifetime 2000
Also make sure that <Directory>
entry in your Apache configuration supports override:
还要确保<Directory>
Apache 配置中的条目支持覆盖:
AllowOverride Options
It also may be possible that you misunderstood the purpose of this option. This option will not set the maximum life time of a session, it will set the amount of time after which the garbage collector will clean the session if it's not valid. This can be determined by a lot of factors, including access times, modification times, other INI options such as session.gc_probabilityand session.gc_divisor.
您也可能误解了此选项的用途。此选项不会设置会话的最大生命周期,它将设置垃圾收集器在会话无效时清理会话的时间量。这可以由很多因素决定,包括访问时间、修改时间、其他 INI 选项,例如session.gc_probability和session.gc_divisor。
If you want to limit a session life time, use a proper mechanism for that, as described by @Gumbo in How do I expire a PHP session after 30 minutes.
如果您想限制会话生存时间,请为此使用适当的机制,如@Gumbo 在How do I expire a PHP session after 30 minutes 中所述。
回答by Fabrizio D'Ammassa
As far I can see, the gc_maxlifetime value can be set anywhere:
据我所知, gc_maxlifetime 值可以在任何地方设置:
http://php.net/manual/en/session.configuration.php
http://php.net/manual/en/session.configuration.php
http://www.php.net/manual/en/configuration.changes.modes.php
http://www.php.net/manual/en/configuration.changes.modes.php
So you could also set it via ini_set(...)
in a php file included in all your pages.
因此,您还可以通过ini_set(...)
所有页面中包含的 php 文件进行设置。
Anyway, I think your code in the .htaccess should work as well. Maybe you missed something else, for example
无论如何,我认为您在 .htaccess 中的代码也应该可以工作。也许你错过了别的东西,例如
php_value session.save_path "/PATH/TO/SESSIONS"
where PATH/TO/SESSIONS is the path of a folder where you have 777 access permissions.
其中 PATH/TO/SESSIONS 是您拥有 777 访问权限的文件夹的路径。