我正在测试PHP会话中安全检查的实现.我可以成功检测会话是否从另一个IP地址启动,我可以成功启动一个新会话.但是,旧会话中的数据会被复制到新会话中!如何在保留其合法所有者的先前会话数据的同时启动空白会话?
在经过多次尝试失败之后,这是我的代码:
// Security check
if( isset($_SESSION['ip_address']) && $_SERVER['REMOTE_ADDR']!=$_SESSION['ip_address'] ){
// Check failed: we'll start a brand new session
session_regenerate_id(FALSE);
$tmp = session_id();
session_write_close();
unset($_SESSION);
session_id($tmp);
session_start();
}
// First time here
if( !isset($_SESSION['ip_address']) ){
$_SESSION['ip_address'] = $_SERVER['REMOTE_ADDR'];
$_SESSION['start_date'] = new DateTime;
}
关于会话的官方文档非常令人困惑:(
更新:我发布了一些经过反复试验的调查结果.他们似乎工作:
// Load the session that we will eventually discard
session_start();
// We can only generate a new ID from an open session
session_regenerate_id();
// We store the ID because it gets lost when closing the session
$tmp = session_id();
// Close session (doesn't destroy data: $_SESSION and file remains)
session_destroy();
// Set new ID for the next session
session_id($tmp);
unset($tmp);
// Start session (uses new ID, removes values from $_SESSION and loads the new ones if applicable)
session_start();