Como definir a vida útil do cookie?


10

Estou tendo problemas para definir a vida útil do cookie na minha instância D8. Gostaria de defini-lo como zero, para que o fechamento do navegador faça o logoff do usuário.

Eu adicionei ini_set('session.cookie_lifetime', 0);ao arquivo site / default / settings.php. Não havia referência cookie_lifetime anterior no arquivo. Eu adicionei a linha. Também limpei o cache do Drupal e limpei o cache do Chrome. Infelizmente, isso não está sendo respeitado. As sessões ainda persistem após o fechamento do navegador.

Pesquisei toda a base de código, ini_set('session.cookie_lifetime', 200000);mas ela não parece existir no meu site. Não vejo onde Drupal está definindo a vida útil do cookie. Eu também tentei adicionar a configuração através de um arquivo php.ini na raiz, mas isso está sendo sobrescrito pelo Drupal.

Eu sinto que isso é uma coisa simples, então eu gostaria de evitar plugins. Ansioso para ouvir de todos. Desde já, obrigado.

Respostas:


18

Para as opções de cookie de sessão, o D8 usa parâmetros de contêiner em vez de configurações. Crie um services.ymlarquivo na mesma pasta que settings.php. Os valores padrão estão em default.services.yml. Você pode copiar este arquivo services.ymle modificá-lo:

/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, muito obrigado. Esta é a solução em que finalmente chegamos.
Tony Stecca

Oi, talvez você conheça alguma maneira de fazer isso dinamicamente?
23819 Артем Ильин -

2
Por favor, você não pode, as opções de cookies são compiladas estaticamente no contêiner. No entanto, você pode trocar o serviço session_configuratione substituir __constructou getOptionsDrupal \ Core \ Session \ SessionConfiguration.
4k4 23/04/19

4к4, muito obrigado pela sua resposta, espero que ajude)
Артем Ильин

Link para a pergunta de acompanhamento drupal.stackexchange.com/questions/279292/…
4k4 13/06/19

-2

Você gostaria de modificar os cookies e os valores da sessão que define #default values ​​para os mesmos valores da sessão ou cookies, caso contrário, não funcionará no 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
Ao utilizar nosso site, você reconhece que leu e compreendeu nossa Política de Cookies e nossa Política de Privacidade.
Licensed under cc by-sa 3.0 with attribution required.