gpt4 book ai didi

php - Codeigniter 中的 session 如何工作

转载 作者:可可西里 更新时间:2023-11-01 13:28:40 25 4
gpt4 key购买 nike

我想弄清楚 session 在 Codeigniter 中是如何工作的。阅读在线手册,我看到以下内容:

If sessions data does not exist (or if it has expired) a new session will be created and saved in the cookie. If a session does exist, its information will be updated and the cookie will be updated. With each update, the session_id will be regenerated.

Note: Session cookies are only updated every five minutes by default to reduce processor load. If you repeatedly reload a page you'll notice that the "last activity" time only updates if five minutes or more has passed since the last time the cookie was written. This time is configurable by changing the $config['sess_time_to_update'] line in your system/config/config.php file.

问题:

  1. 如果在加载具有 session 类的页面时存在 session ,会更新哪些信息?这是存储在 cookie 中的 session ID,还是存储在数据库中的 session 数据本身?
  2. session cookie 仅每 5 分钟更新一次。如果用户在 5 分钟内从页面 A 转到页面 B,这需要添加新的 session 数据怎么办?从逻辑上讲, session 数据应该更新,所以我想我错误地理解了这一行......在这种情况下,我猜测 session cookie 每 5 分钟获取一个新的 session ID。

任何澄清都会有所帮助!

最佳答案

是的,是关于cookie中存储的session id。每 5 分钟重新生成一次。当需要重新生成时,首先它将获取当前 session 数据,然后将其分配给新的 session ID。

来自 CI session 库的代码,函数 sess_update():

// Save the old session id so we know which record to
// update in the database if we need it
$old_sessid = $this->userdata['session_id'];
$new_sessid = '';
while (strlen($new_sessid) < 32)
{
$new_sessid .= mt_rand(0, mt_getrandmax());
}

// To make the session ID even more secure we'll combine it with the user's IP
$new_sessid .= $this->CI->input->ip_address();

// Turn it into a hash
$new_sessid = md5(uniqid($new_sessid, TRUE));

// Update the session data in the session data array
$this->userdata['session_id'] = $new_sessid;
$this->userdata['last_activity'] = $this->now;

关于php - Codeigniter 中的 session 如何工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9242017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com