Làm cách nào để đặt tuổi thọ cookie?


10

Tôi gặp sự cố khi đặt tuổi thọ cookie trong phiên bản D8 của mình. Tôi muốn đặt nó về 0 để đóng trình duyệt thoát khỏi người dùng.

Tôi đã thêm vào ini_set('session.cookie_lifetime', 0);tệp site / default / settings.php. Không có tài liệu tham khảo cookie_lifetime trước đó trong tệp. Tôi thêm dòng. Tôi cũng đã xóa bộ nhớ cache Drupal và xóa bộ nhớ cache Chrome của mình. Đáng buồn thay, nó không được tôn trọng. Phiên vẫn tồn tại sau khi đóng trình duyệt.

Tôi đã tìm kiếm toàn bộ cơ sở mã cho ini_set('session.cookie_lifetime', 200000);nhưng nó dường như không tồn tại trong trang web của tôi. Tôi không thấy Drupal đang thiết lập vòng đời cookie. Tôi cũng đã thử thêm cài đặt qua tệp php.ini trong thư mục gốc nhưng điều đó bị Drupal cai trị quá mức.

Tôi cảm thấy như đây là một điều đơn giản, vì vậy tôi muốn tránh các plugin. Mong muốn được nghe từ tất cả mọi người. Cảm ơn trước.

Câu trả lời:


17

Đối với các tùy chọn cookie phiên D8 sử dụng tham số vùng chứa thay vì cài đặt. Tạo một services.ymltập tin trong cùng một thư mục như settings.php. Các giá trị mặc định là trong default.services.yml. Bạn có thể sao chép tệp này vào services.ymlvà sửa đổi nó:

/sites/default/service.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, cảm ơn rất nhiều. Đây là giải pháp cuối cùng chúng tôi đã hạ cánh.
Tony Stecca

Xin chào, có lẽ bạn biết cách nào để làm điều đó một cách linh hoạt?
Đăng nhập

2
@ Bạn không thể, các tùy chọn cookie được biên dịch tĩnh vào vùng chứa. Tuy nhiên, bạn có thể trao đổi dịch vụ session_configurationvà ghi đè __constructhoặc getOptionscủa Drupal \ Core \ Session \ SessionConfiguration.
4k4

4к4, cảm ơn rất nhiều vì câu trả lời của bạn, hy vọng nó có ích)
Tiếng Anh

Liên kết đến câu hỏi tiếp theo drupal.stackexchange.com/questions/279292/ trên
4k4

-2

Bạn muốn sửa đổi cookie và giá trị phiên bạn đặt giá trị #default thành cùng giá trị của phiên hoặc cookie nếu không nó sẽ không hoạt động trong 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
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.