时间:2021-07-01 10:21:17 帮助过:24人阅读
session_start();
$_SESSION['visits']++;
print'You have visited here '.$_SESSION['visits'].' times.
';
echo'session id = '.$_COOKIE['PHPSESSID'];
echo"
";
echo"session name = ".session_name()."
";
?>
SessionID记录在全局变量_COOKIE中,SessionID的名字是PHPSESSID,PHPSESSID也可以通过session_name()获得。
2 预防Session劫持
ini_set('sessio.use_only_cookies', true);
session_start();
$salt = 'YourSpecialValueHere';
$tokenstr = date('W').$salt;
$token = md5($tokenstr);
echo'token = '.$token.'
';
if(!isset($_REQUEST['token']) || $_REQUEST['token'] != $token)
{
exit;
}
$_SESSION['token'] = $token;
output_add_rewrite_var('token', $token);
echo'link';
ob_flush();
output_reset_rewrite_vars();
?>
session_start();
output_add_rewrite_var('var', 'value');
echo'link';
ob_flush();
output_reset_rewrite_vars();
echo'link';
?>以上例程会
输出:
<ahref="file.php?PHPSESSID=xxx&var=value">linka><ahref="file.php">linka>3 预防Session定制
ini_set('session.use_only_cookie', true);
session_start();
if(!isset($_SESSION['generated']) || $_SESSION['generated'] < (time() - 30))
{
session_regenerate_id();
$_SESSION['generated'] = time();
}
echo$_COOKIE['PHPSESSID']
').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i <= lines; i++) {
$numbering.append($('').text(i));
};
$numbering.fadeIn(1700);
});
});
以上就介绍了11 Session和数据保持,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。