【问题标题】:How Sessions in Codeigniter WorkCodeigniter 中的会话如何工作
【发布时间】:2012-03-03 18:18:15
【问题描述】:

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

如果会话数据不存在(或已过期),将创建一个新会话并将其保存在 cookie 中。如果会话确实存在,则其信息将被更新并且 cookie 将被更新。每次更新都会重新生成 session_id。

注意:默认情况下,会话 cookie 仅每五分钟更新一次,以减少处理器负载。如果您反复重新加载页面,您会注意到“上次活动”时间仅在自上次写入 cookie 后经过五分钟或更长时间时才会更新。这个时间可以通过更改 system/config/config.php 文件中的 $config['sess_time_to_update'] 行来配置。

问题

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

任何澄清都会有所帮助!

【问题讨论】:

  • 我也很困惑。我必须在 CI 1.7.2 上说一个奇怪的行为。当我使用 DB 类型会话应用程序工作正常。但如果 DB 类型为 false,则应用程序非常慢。有什么建议吗?

标签: php codeigniter session cookies


【解决方案1】:

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

来自 CI 会话库的代码,函数 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;

【讨论】:

  • 非常感谢您提供这些有用的信息,以及对编程的热爱。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-13
  • 1970-01-01
  • 2013-08-29
  • 1970-01-01
  • 2017-06-15
相关资源
最近更新 更多