在一个浏览器的cookies为不同的用户 [英] Cookies for different user in one browser

查看:146
本文介绍了在一个浏览器的cookies为不同的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一定的下拉网站的页面上(用户只能访问他/她进行身份验证此页)一个我要救这个值的cookie,并设置回降下来的时候用户得到回我的网站。

There is some drop down on the page of web site (user has access to this page only if he/she is authenticated) an I want to save this value to cookies and set it back to drop down when user gets back to my site.

这是不保存当前选择的下拉选项值cookie,并在以后检索它的一个问题。不过,我面临着一些问题,如果让我在登录其他用户相同的浏览器。它从饼干值什么救了previous用户。

It is not a problem to save currently selected drop down option value to cookie and retrieve it later. But I faced with some problem if I make login at the same browser by another user. It gets from cookies value what was saved by previous user.

那么,什么是从同一个浏览器中分离为不同用户的cookies好办法吗?我在想创建一个像'用户名 - 下拉菜单的名字Cookie,但我有些怀疑,这是最好的解决方案。

So what is good way to separate cookies for different users from the same browser? I was thinking about create cookie with name like 'username-dropdown' but I have some doubts that it is the best solution.

我使用Java和Tomcat 8。

I use Java with Tomcat 8.

推荐答案

我要在这里,你的来猜测不的需要发送到您的服务器的每一个信息HTTP请求;你只需要存储的信息的客户端(你可以根据需要通过AJAX发送到服务器)。

I'm going to take a guess here that you don't need this information sent to your server with each and every HTTP request; you just need to store the information client-side (and you can send it to the server as necessary via ajax).

如果是这样,我会使用本地存储,而不是饼干。而且肯定的是,使用用户名或用户ID或一些这样的合理性:

If so, I'd use local storage, not cookies. And sure, using the username or user ID or some such is reasonable:

// Setting:
localStorage.setItem(username + "-dropdown", value);

// Getting:
var value = localstorage.getItem(username + "-dropdown");

或者你也可以使用方括号表示法:

Or you can use brackets notation:

// Setting:
localStorage[username + "-dropdown"] = value;

// Getting:
var value = localstorage[username + "-dropdown"];

pretty太多不使用括号标记的唯一理由是,如果你需要填充工具对旧的浏览器本地存储(有一些回落,以饼干为你polyfills),但本地存储的支持所有现代浏览器的,也是IE8,所以这些浏览器真的非常过时在这一点上。

Pretty much the only reason not to use brackets notation is if you need to polyfill local storage on older browsers (there are polyfills that fall back to cookies for you), but local storage is supported on all modern browsers, and also IE8, so those browsers really are very out of date at this point.

这篇关于在一个浏览器的cookies为不同的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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