为什么在CodeIgniter中需要更新会话ID.我知道您可以控制在配置中更新会话ID的频率.但为什么他们每5分钟更改一次会话的ID(默认情况下)?
为什么会话无法创建一次,并且在会话到期之前使用相同的ID?
更新会话的功能如下:
/**
* Update an existing session
*
* @access public
* @return void
*/
function sess_update()
{
// We only update the session every five minutes by default
if (($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now)
{
return;
}
// 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;
// _set_cookie() will handle this for us if we aren't using database sessions
// by pushing all userdata to the cookie.
$cookie_data = NULL;
// Update the session ID and last_activity field in the DB if needed
if ($this->sess_use_database === TRUE)
{
// set cookie explicitly to only have our session data
$cookie_data = array();
foreach (array('session_id','ip_address','user_agent','last_activity') as $val)
{
$cookie_data[$val] = $this->userdata[$val];
}
$this->CI->db->query($this->CI->db->update_string($this->sess_table_name, array('last_activity' => $this->now, 'session_id' => $new_sessid), array('session_id' => $old_sessid)));
}
// Write the cookie
$this->_set_cookie($cookie_data);
}
Run Code Online (Sandbox Code Playgroud)
这是出于安全原因以防止欺骗。会话被加密并存储到您的 Cookie 中。任何人都可以复制您的 Cookie 并转到另一台电脑并登录。
假设会话不会过期。这意味着,如果我访问您的 PC 并窃取您的 cookie,我可以从任何地方登录,因为我在 Cookie 中加密了您的会话 ID。如果框架每 5 分钟更新一次会话,这几乎是不可能发生的。
如果您不喜欢这种工作方式,您可以随时通过扩展 Codeigniter 核心系统的库来创建自己的会话库。尽管不建议这样做。
| 归档时间: |
|
| 查看次数: |
1262 次 |
| 最近记录: |