Php 为什么在CodeIgniter中调用sess_更新?
为什么需要在CodeIgniter中更新会话Id。我知道您可以控制会话id在配置中的更新频率。但是为什么他们每5分钟更改一次会话id(默认情况下) 为什么不能创建一次会话并在会话过期之前使用相同的id 更新会话的功能如下所示:Php 为什么在CodeIgniter中调用sess_更新?,php,codeigniter,Php,Codeigniter,为什么需要在CodeIgniter中更新会话Id。我知道您可以控制会话id在配置中的更新频率。但是为什么他们每5分钟更改一次会话id(默认情况下) 为什么不能创建一次会话并在会话过期之前使用相同的id 更新会话的功能如下所示: /** * Update an existing session * * @access public * @return void */ function sess_update() { // We only update the session
/**
* 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);
}
/**
*更新现有会话
*
*@access-public
*@返回无效
*/
函数sess_update()
{
//默认情况下,我们仅每五分钟更新一次会话
如果($this->userdata['last\u activity']+$this->sess\u time\u to\u update)>=$this->now)
{
返回;
}
//保存旧会话id,以便我们知道要保存哪个记录
//如果需要,请在数据库中进行更新
$old_sessiond=$this->userdata['session_id'];
$new_sessiond='';
while(strlen($new_sessiond)<32)
{
$new_sessiond.=mt_rand(0,mt_getrandmax());
}
//为了使会话ID更加安全,我们将它与用户的IP相结合
$new_sessiond.=$this->CI->input->ip_address();
//把它变成一个杂烩
$new_sessiond=md5(uniqid($new_sessiond,TRUE));
//更新会话数据数组中的会话数据
$this->userdata['session\u id']=$new\u sessiond;
$this->userdata['last_activity']=$this->now;
//如果我们不使用数据库会话,则\u set\u cookie()将为我们处理此问题
//通过将所有用户数据推送到cookie。
$cookie_data=NULL;
//如果需要,更新数据库中的会话ID和最后一个活动字段
如果($this->sess\u use\u database===TRUE)
{
//将cookie显式设置为仅包含会话数据
$cookie_data=array();
foreach(数组('session_id'、'ip_address'、'user_agent'、'last_activity')为$val)
{
$cookie_data[$val]=$this->userdata[$val];
}
$this->CI->db->query($this->CI->db->update_string($this->sess_table_name,数组('last_activity'=>this->$now,'session_id'=>$new_sessiond),数组('session_id'=>$old_sessiond));
}
//写曲奇
$this->\u set\u cookie($cookie\u数据);
}
这是一项安全功能
谷歌
会话劫持
会话固定
例如,当用户登录到您的系统时,您至少应该更改会话id
应为通过登录创建新会话的用户分配
使用session_regenerate_ID函数刷新会话ID。劫持
用户将尝试在登录前设置其会话ID;这可能是
如果在登录时重新生成ID,则会阻止此操作
您可以在此处阅读有关会话安全性的更多信息:
这是出于安全原因,以防止欺骗。会话被加密并存储到cookie中。任何人都可以复制你的Cookie,然后转到另一台电脑并登录 假设会话没有过期。这意味着,如果我在你的电脑上偷了你的cookie,我可以从任何地方登录,因为我在cookie中加密了你的会话id。如果框架每5分钟更新一次会话,这几乎不可能发生
如果您不喜欢这种工作方式,您可以通过扩展Codeigniter核心系统中的库来创建自己的会话库。尽管不建议这样做。我认为这有助于防止会话劫持,但如果会话每5分钟更改一次ID,攻击者劫持会话的难度就会大得多。可能还有其他原因,但我记不清了。我想了这么多。那是给commet的。