浏览器禁用 cookie 时使用 http header etag 保存会话信息

/* 禁用cookie和session时,使用 ETag 保存会话信息
$session = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? unserialize(base64_decode($_SERVER['HTTP_IF_NONE_MATCH'])) : array('counter'=>0);
$session['counter']++;
header('ETag: '.base64_encode(serialize($session)));
echo $session['counter'];
*/

// 客户端禁用cookie时,使用 ETag 保存 session_id
ini_set('session.use_cookies', 0);
if(isset($_SERVER['HTTP_IF_NONE_MATCH'])){
	$session_id = $_SERVER['HTTP_IF_NONE_MATCH'];
	session_id($session_id);
	session_start();
}else{
	session_start();
	$session_id = session_id();
}

if(phpversion() >= 5.3){
	header_remove('Cache-Control');
}else{
	header('Cache-Control:');
}

header('ETag: '.$session_id);
$_SESSION['counter'] = isset($_SESSION['counter']) ? $_SESSION['counter']+1 : 1;
echo $_SESSION['counter'];