CodeIgniter 4会话在刷新网站后被销毁

l5tcr1uw  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(147)

我的会话总是在刷新页面后被销毁。
这是我的设定。

app.sessionDriver = 'CodeIgniter\Session\Handlers\DatabaseHandler'
app.sessionCookieName = 'sippeg_session'
app.sessionExpiration = 0
app.sessionSavePath = 'ci_sessions'
app.sessionMatchIP = false
app.sessionTimeToUpdate = 300
app.sessionRegenerateDestroy = false

我没有任何导致session_destroyed的代码。
在重定向之前,我的会话仍然可用:

但当我刷新页面时,会话已消失:

wfsdck30

wfsdck301#

说明:

CodeIgniter 4会话首选项
如果sessionExpiration被设置为0,PHP在会话管理中设置的session.gc_maxlifetime设置将被原样使用(通常是1440的默认值)。这需要根据需要在php.ini中或通过ini_set()进行更改。
摘录自php.ini文件(会话.cookie_寿命)

; Lifetime in seconds of cookie or, if 0, until browser is restarted.
; https://php.net/session.cookie-lifetime
session.cookie_lifetime = 0

摘录自php.ini文件(会话.gc-maxlifetime)

; After this number of seconds, stored data will be seen as 'garbage' and
; cleaned up by the garbage collection process.
; https://php.net/session.gc-maxlifetime
session.gc_maxlifetime = 1440

; NOTE: If you are using the subdirectory option for storing session files
;       (see session.save_path above), then garbage collection does *not*
;       happen automatically.  You will need to do your own garbage
;       collection through a shell script, cron entry, or some other method.
;       For example, the following script is the equivalent of setting
;       session.gc_maxlifetime to 1440 (1440 seconds = 24 minutes):
;          find /path/to/sessions -cmin +24 -type f | xargs rm

溶液:

由于您通常无法访问shared web hosting services上的php.ini文件来配置session.gc_maxlifetime,因此直接在项目根路径下的.env文件中进行设置会更方便。例如:
代替:

app.sessionExpiration = 0 ❌

使用此选项:

  • 时间以秒为单位。86400 = 24小时。*
app.sessionExpiration = 86400 ✅
cookie.expires = 86400

相关问题