在移动会话中模拟会话 cookie? [英] Simulate session cookies in mobile sessions?

查看:20
本文介绍了在移动会话中模拟会话 cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

乍一看,我惊讶地发现,我对会话 cookie 在移动设备上的行为的想法被现实推翻了.

I discovered to my astonishment at the first glance that my thinking of how session cookies behave on mobile devices is overruled by reality.

在普通桌面浏览器上,只要浏览器会话处于活动状态,就会存储会话 cookie.如果最后一个浏览器窗口/进程关闭,会话应该关闭.

On normal desktop browsers the behavior is to store a session cookie as long as the browser session is active. The session should be closed, if the last browser window/process is closed.

现在在移动设备上,您几乎不需要关闭浏览器应用程序,只需将其发送到后台即可.

Now on mobile devices you hardly ever close a browser app, you just send it to the background.

我在装有 Android 4 的 Sony Xperia Ray 上发现会话 cookie 没有过期,即使我清除了浏览器进程.但在三星平板设备上它会.我不知道 iOS 设备的行为方式.

I discovered on my Sony Xperia Ray with Android 4 that the session cookie is not expired, even if I purge the browser process. But on a Samsung Tablet device it would. I don't know, how iOS devices behave in that way.

这是个问题!?我应该怎么做才能解决它?

This is a problem!? What should I do to work around it?

现在,我决定让 cookie 在一天后过期.但我对此并不满意.
我应该降低寿命吗?也许到 8 小时?

For now, I decided to let the cookie expire in one day. But I'm not very happy with that.
Should I lower the lifetime? Perhaps to 8 hours?

推荐答案

采用 HTML5 方式并使用 sessionStorage 对您来说有意义吗?

Would it make sense for you to go the HTML5 way and use sessionStorage?

通过这种方式,您可以独立于不同设备处理浏览器会话的方式,因为 HTML5 会话存储是按窗口进行的,因此它仅限于浏览器窗口的生命周期.

This way you could be independent of the way different devices handle browser sessions, since HTML5 session storage is per-window, thus it is limited to the lifetime of the browser window.

基本上所有移动设备都支持 sessionStorage(请参阅此处)和你可以有一个像 jQuery-Session-Plugin 这样的框架/插件(按照 这个链接) 为您处理会话数据(并为不支持 sessionStorage 的旧浏览器提供回退会话 cookie).

Basically all mobile devices support sessionStorage (see here) and you could have a framework/plugin like jQuery-Session-Plugin (follow this link) handle the session data for you (and provide a fallback to session cookies for old browsers that don't support sessionStorage).

编辑:为了显示 sessionStorage 与 localStorage 的行为,我创建了一个小提琴(出于演示目的)使用 sessionStorage 来存储 div 的宽度和 localStorage 用于存储同一div的高度:

EDIT: In order to show the behavior of sessionStorage vs. localStorage, I've created a fiddle that (for demonstration purpose) uses sessionStorage for storing the width of a div and localStorage for storing the height of the same div:

var randomWidth,
    randomHeight;
if (!(randomWidth= $.session.get("randomWidth"))) {    // assignment
    randomWidth = Math.random() * 300;
    $.session.set("randomWidth", randomWidth, true);
    console.log("just assigned and stored in sessionStorage: randomWidth: " + randomWidth);
} else {
    console.log("from sessionStorage: randomWidth: " + randomWidth);
}
if (!(randomHeight= $.domain.get("randomHeight"))) {    // assignment
    randomHeight = Math.random() * 300;
    $.domain.set("randomHeight", randomHeight, true);
    console.log("just assigned and stored in localStorage: randomHeight: " + randomHeight);
} else {
    console.log("from localStorage: randomHeight: " + randomHeight);
}
$(".test").css({width: randomWidth, height: randomHeight});

看看控制台.您会看到,当您启动客户端浏览器的新会话时,宽度会发生变化,而高度会保持不变(因为 local 存储是每个域的).

Look at the console. You will see that when you initiate a new session of your client browser, the width will variate while the height will stay the same (because local Storage is per domain).

这是 jsfiddle 链接

这篇关于在移动会话中模拟会话 cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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