我可以通过哪些方式强制结束会话?
我想强制会话在 30 分钟后结束,无论活动如何。我首先想到的是这个(未经测试):
function onApplicationStart() application.sessionLife = 30;
function onSessionStart() session.timeStarted = now();
function onRequestStart() {
if ( dateDiff("n", session.timeStarted, now()) > application.sessionLife)
structClear(session);
}
还有什么其他方法可以强制会话结束?
最明显的一个是 30 分钟后过期的会话 cookie。但我不确定会话 cookie 是否监听 sessionTimeOut 会话。
更新 我刚刚发现 StructClear() 不会结束会话。所以我想我应该让cookie过期。
I want to force a session to end after 30 minutes regardless of activity. The first thing I thought of was this (untested):
function onApplicationStart() application.sessionLife = 30;
function onSessionStart() session.timeStarted = now();
function onRequestStart() {
if ( dateDiff("n", session.timeStarted, now()) > application.sessionLife)
structClear(session);
}
What other ways are available to force a session to end?
The obvious one is a session cookie that expires after 30 minutes. But I'm not sure if session cookies listen to the sessionTimeOut sessting or not.
Update
I just found out that StructClear() does not end the session. So I guess I should just expire the cookies.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像往常一样,Ben Nadel 已经在博客中给出了答案...
http://www.bennadel.com/blog/1847-Explicitly-Ending-A-ColdFusion-Session.htm
有几个选项,有些非常安全,其他则使用隐藏方法不保证在 ColdFusion 或其他 CFML 引擎的未来版本中出现。您可能会发现我在他的帖子中被提及,因为我喜欢摆弄那些隐藏的方法。但如果你想正确地做事(更多的未来证明/引擎切换证明),我建议本的替代方法。
编辑
差点忘了,我使用另一种方法创建了一个函数来停止当前会话(Ben Nadel 的博客文章中没有提到)。它仍然是一个未记录的功能,但是是一项改进,因为它会立即停止会话,而不是将其设置为 1 秒超时。
http://misterdai.wordpress.com/ 2010/06/15/cf-sessionstop-ending-a-cf-session/
进一步编辑
只是简单地解释一下不同的方法,以防链接的网站出现消失。
总结一下停止会话的方法...
if (StructKeyExists(session, 'remove') 和 aession.remove) {
this.sessionTimeout = CreateTimeSpan(0, 0, 0, 1);
}
方法 3 的代码...
As usual, Ben Nadel has already blogged the answer...
http://www.bennadel.com/blog/1847-Explicitly-Ending-A-ColdFusion-Session.htm
There are a few options, some quite safe, others use hidden methods that aren't guaranteed to be around in future version of ColdFusion or other CFML engines. You might spot that I get a mention in his post since I love messing about with those hidden methods. But if you want to do things properly (more future proof / engine switch proof), I'd suggest Ben's alternative methods.
Edit
Almost forgot, I created a function to stop the current session using another method (not mentioning in Ben Nadel's blog post). It's still an undocumented feature, but is an improvement as it stops the session instantly, instead of setting it to 1 second to time out.
http://misterdai.wordpress.com/2010/06/15/cf-sessionstop-ending-a-cf-session/
Further Edit
Just explaining the different methods briefly, in case the linked sites ever disapppear.
To summarise the ways to stop a session in it's tracks...
if (StructKeyExists(session, 'remove') And aession.remove) {
this.sessionTimeout = CreateTimeSpan(0, 0, 0, 1);
}
Code for approach 3...
我认为您不想使用 structClear()。如果我没记错的话,如果从 SESSION 结构中删除 CFID 和 CFTOKEN 键,就会出现问题。下面是我们用来结束会话的代码:
因此,我们从 SESSION 中删除除 CFID、CFTOKEN、SESSIONID 和 URLTOKEN 之外的所有密钥,然后使标识会话的 cookie 过期。发生这种情况时,CF 应创建新的 cookie 并启动新会话。
I don't think you want to use structClear(). If I remember correctly, there are issues if the CFID and CFTOKEN keys get deleted from the SESSION struct. Here is the code we use to end a session:
So we delete all the keys from the SESSION except for CFID, CFTOKEN, SESSIONID, and URLTOKEN, then we expire the cookies that identify the session. When this happens CF should create new cookies and start a new session.