session_regenerate_id(true) 对 ajax 请求或快速刷新的无效会话 [英] session_regenerate_id(true) invalid session on ajax requests or fast refresh

查看:36
本文介绍了session_regenerate_id(true) 对 ajax 请求或快速刷新的无效会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了避免会话固定,我在每个 PHP 页面的开头使用此代码:

To avoid session fixation i use this code at the beginning of every PHP page:

session_set_cookie_params( 900, '/', $domain, 1, 1 );
session_start();
session_regenerate_id( true );

但是如果页面刷新过快或者有多个ajax请求,session id就会失效.

But if the page is refreshed too fast or in case of multiple ajax requests, the session id becomes invalid.

有没有办法在没有这个问题的情况下避免会话固定?

There is a way to avoid session fixation without this problem?

推荐答案

使用 http_only cookie 标志,这将防止通过 xss 攻击劫持您的会话 ID.几乎所有现代浏览器都支持它.对于较旧的浏览器,请确保您的代码中没有 xss 漏洞.如果可能,还可以使用安全标志,以在网络层对其进行保护.

Use http_only cookie flag, which will prevent the hijacking of your session id through xss attacks. It is supported in almost all modern browsers. For older browsers make sure you don't have xss vulnerability in your code. Also use the secure flag, if possible to secure it on network layer.

void session_set_cookie_params ( int $lifetime [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]] )

您也可以按时间或计数重新生成.希望能帮到你!

You can also regenerate on time basis or count basis. Hope it helps!

这篇关于session_regenerate_id(true) 对 ajax 请求或快速刷新的无效会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆