简体   繁体   English

Codeigniter中的会话如何工作

[英]How Sessions in Codeigniter Work

I am trying to figure out how sessions work in Codeigniter. 我想弄清楚会话如何在Codeigniter中工作。 Reading the online manual, I see the following: 阅读在线手册,我看到以下内容:

If sessions data does not exist (or if it has expired) a new session will be created and saved in the cookie. 如果会话数据不存在(或者它已过期),则会创建一个新会话并将其保存在cookie中。 If a session does exist, its information will be updated and the cookie will be updated. 如果会话确实存在,则会更新其信息并更新cookie。 With each update, the session_id will be regenerated. 每次更新时,都会重新生成session_id。

and

Note: Session cookies are only updated every five minutes by default to reduce processor load. 注意:默认情况下,会话cookie每五分钟更新一次,以减少处理器负载。 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. 如果您反复重新加载页面,您会注意到“上次活动”时间仅在自上次写入cookie后经过五分钟或更长时间后才会更新。 This time is configurable by changing the $config['sess_time_to_update'] line in your system/config/config.php file. 可以通过更改system / config / config.php文件中的$ config ['sess_time_to_update']行来配置此时间。

Question : 问题

  1. What information is updated if a session exists when a page with session class is loaded? 如果加载了具有会话类的页面时存在会话,则会更新哪些信息? Is this the session id stored in the cookie, or the session data itself stored in the database? 这是会话ID存储在cookie中,还是会话数据本身存储在数据库中?
  2. Session cookies are only updated every 5 minutes. 会话cookie仅每5分钟更新一次。 What if the user goes from page A to page B within 5 minutes, and this requires the addition of new session data? 如果用户在5分钟内从页面A转到页面B,这需要添加新的会话数据,该怎么办? Logically the session data should be updated, so I guess I'm understanding this line wrongly... In this case, I will guess that the session cookie gets a new session id every 5 minutes. 逻辑上会话数据应该更新,所以我想我错误地理解了这一行...在这种情况下,我猜测会话cookie每5分钟获得一个新的会话ID。

Any clarifications will help! 任何澄清都会有所帮助!

Yes, is about the session id stored in the cookie. 是的,是关于存储在cookie中的会话ID。 This is regenerated every 5 minutes. 每5分钟重新生成一次。 And when it's time to regenerate, first it will get current session data and than assign it to the new session id. 当需要重新生成时,首先它将获取当前会话数据,然后将其分配给新的会话ID。

code from CI session library, function sess_update(): 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM