如何设置Cookie寿命?


10

我在D8实例中设置Cookie生存期时遇到问题。我想将其设置为零,以便关闭浏览器注销用户。

我已添加ini_set('session.cookie_lifetime', 0);到site / default / settings.php文件。该文件中没有以前的cookie_lifetime参考。我加了线。我还清除了Drupal缓存并清除了我的Chrome缓存。可悲的是,它没有得到尊重。在浏览器关闭后,会话仍然存在。

我已经搜索了整个代码库,ini_set('session.cookie_lifetime', 200000);但是在我的网站中似乎不存在。我看不到Drupal在哪里设置cookie寿命。我还尝试通过根目录中的php.ini文件添加设置,但是Drupal否决了该设置。

我觉得这很简单,所以我想避免使用插件。期待大家的来信。提前致谢。

Answers:


18

对于会话cookie选项,D8使用容器参数代替设置。services.yml在与相同的文件夹中创建一个文件settings.php。默认值为default.services.yml。您可以将此文件复制到services.yml并进行修改:

/sites/default/services.yml:

parameters:
  session.storage.options:
    # Default ini options for sessions.
    #
    # Some distributions of Linux (most notably Debian) ship their PHP
    # installations with garbage collection (gc) disabled. Since Drupal depends
    # on PHP's garbage collection for clearing sessions, ensure that garbage
    # collection occurs by using the most common settings.
    # @default 1
    gc_probability: 1
    # @default 100
    gc_divisor: 100
    #
    # Set session lifetime (in seconds), i.e. the time from the user's last
    # visit to the active session may be deleted by the session garbage
    # collector. When a session is deleted, authenticated users are logged out,
    # and the contents of the user's $_SESSION variable is discarded.
    # @default 200000
    gc_maxlifetime: 200000
    #
    # Set session cookie lifetime (in seconds), i.e. the time from the session
    # is created to the cookie expires, i.e. when the browser is expected to
    # discard the cookie. The value 0 means "until the browser is closed".
    # @default 2000000
    cookie_lifetime: 2000000

4k4,非常感谢。这是我们最终着手的解决方案。
托尼·史蒂卡

嗨,也许您知道有什么动态方式吗?
АртемИльин

2
@АртемИльин,您不能,cookie选项是静态编译到容器中的。但是,您可以交换服务session_configuration和覆盖__constructgetOptionsDrupal的\核心\会议\ SessionConfiguration的。
4k4,9

4к4,非常感谢您的回答,希望它有助于)
АртемИльин


-2

您想要修改cookie和会话值,并将#default值设置为相同的session或cookie值,否则在drupal 8中将无法使用

**Ex : #default 0
gc_maxlifetime: 0**

parameters:
  session.storage.options:
    # Default ini options for sessions.
    #
    # Some distributions of Linux (most notably Debian) ship their PHP
    # installations with garbage collection (gc) disabled. Since Drupal depends
    # on PHP's garbage collection for clearing sessions, ensure that garbage
    # collection occurs by using the most common settings.
    # @default 1
    gc_probability: 1
    # @default 100
    gc_divisor: 100
    #
    # Set session lifetime (in seconds), i.e. the time from the user's last
    # visit to the active session may be deleted by the session garbage
    # collector. When a session is deleted, authenticated users are logged out,
    # and the contents of the user's $_SESSION variable is discarded.
    # @default 200000
    gc_maxlifetime: 200000
    #
    # Set session cookie lifetime (in seconds), i.e. the time from the session
    # is created to the cookie expires, i.e. when the browser is expected to
    # discard the cookie. The value 0 means "until the browser is closed".
    # @default 2000000
    cookie_lifetime: 2000000
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.