![选择偏好](https://i-blog.csdnimg.cn/blog_migrate/d7d275ef1ea778ba7dd85fa50281c4a7.png)
选择偏好
![how-does-a-website-remember-your-preferences-for-it-00 how-does-a-website-remember-your-preferences-for-it-00](https://i-blog.csdnimg.cn/blog_migrate/912d2a509f3fb98fe5445d624468b44a.png)
How do websites remember your preferences for them (or desire for none), and what about the touchy subject of cookies themselves? Today’s SuperUser Q&A post seeks to shed some light on how cookies work and the information they store for a confused reader.
网站如何记起您对它们的喜好(或无所求),以及cookie本身的敏感主题如何? 今天的SuperUser Q&A帖子旨在阐明Cookie的工作方式以及它们为困惑的读者存储的信息。
Today’s Question & Answer session comes to us courtesy of SuperUser—a subdivision of Stack Exchange, a community-driven grouping of Q&A web sites.
今天的“问答”环节由SuperUser提供,它是Stack Exchange的一个分支,该社区是由社区驱动的Q&A网站分组。
Photo courtesy of Pedro Vezini (Flickr).
问题 (The Question)
SuperUser reader Ruud Lenders wants to know more about how preferences and cookies for websites work:
超级用户读者Ruud Lenders想了解有关网站首选项和cookie的工作方式的更多信息:
A pop-up displayed on a website that I visited and asked me if I would allow the website to store preferences in cookies. By accident, I chose no. Refreshing the page does not bring the pop-up back. Is there a way to get this kind of pop-up back without clearing the history and cookies?
我访问过的网站上显示一个弹出窗口,并询问我是否允许该网站将首选项存储在cookie中。 偶然,我选择了不。 刷新页面不会使弹出窗口返回。 有没有办法在不清除历史记录和Cookie的情况下恢复这种弹出式窗口?
This also got me to thinking. How can a website remember if it is allowed to store cookies? By storing it in a cookie?
这也让我思考。 网站如何记住是否允许其存储Cookie? 通过将其存储在cookie中?
How does a website remember your preferences for it overall and about your choices regarding cookies in general?
网站如何记起您对网站的总体偏好以及关于Cookie的一般选择?
答案 (The Answer)
SuperUser contributor bvukelic has the answer for us:
超级用户贡献者bvukelic为我们提供了答案:
They asked you if you would like to store preferences in cookies, not if you would like them to set cookies in general. So if I were writing support for this feature, I would set a separate cookie (nopref) and check whether or not the user has this cookie. There is a good chance you will find such a cookie for that website, which you can clear without removing other cookies or history.
他们问您是否要将首选项存储在cookie中,而不是是否希望他们通常设置cookie。 因此,如果要编写对此功能的支持,我将设置一个单独的cookie( nopref ),并检查用户是否具有此cookie。 您很有可能会在该网站上找到这样的cookie,您可以在不删除其他cookie或历史记录的情况下清除它。
Inspecting Cookies Set for a Particular Page
检查特定页面的Cookie集
In Firefox, you can list cookies for a particular web page by right-clicking a blank part of the page, then selecting the View Page Info option. You will find a View Cookies button in the Security Tab. In Chrome, you have the same View Page Info option which opens a dialog that hangs from the address bar. A link near the top will take you to a listing of cookies. I assume similar features can be found in other browsers.
在Firefox中,您可以通过右键单击页面的空白部分,然后选择“ 查看页面信息”选项来列出特定网页的Cookie。 您将在“ 安全性”选项卡中找到“ 查看Cookies”按钮。 在Chrome浏览器中,您具有相同的“ 查看页面信息”选项,该选项可打开一个从地址栏挂起的对话框。 顶部附近的链接将带您进入Cookie列表。 我假设可以在其他浏览器中找到类似的功能。
Discovering if a Website Sets a Cookie
发现网站是否设置Cookie
Here is one way that you can discover what the website is doing. Visit the website in Incognito Mode. Open the Developer Tools and switch to the Network Tab. Then check to see what activity is occurring in the background as you decline having the site track you with cookies. Specifically, look for response headers and see if there are any Set-Cookie headers in there. Then try deleting cookies mentioned in the header to see if that makes any difference.
这是您发现网站正在做什么的一种方法。 以隐身模式访问该网站。 打开开发人员工具,然后切换到“ 网络”选项卡 。 然后检查一下,当您拒绝让网站使用Cookie跟踪您时,后台会发生什么活动。 具体来说,查找响应标头,然后查看其中是否有Set-Cookie标头。 然后尝试删除标题中提到的cookie,以查看是否有任何区别。
About localStorage
关于localStorage
Since another poster has mentioned localStorage (in-browser database), I will comment on this as well. I think it will be very rare that a website uses localStorage for this purpose as localStorage data is not accessible to the server unless there is JavaScript code that sends the data back to the server. If you wish to check the contents of localStorage, the fastest way is to open the Developer Tools in your browser, go to the (JavaScript) Console Tab, and type localStorage. This should give you an output that looks something like:
由于另一个发布者提到了localStorage(浏览器内数据库),因此我也会对此发表评论。 我认为网站很少使用localStorage来实现此目的,因为除非有JavaScript代码将数据发送回服务器,否则服务器无法访问localStorage数据。 如果要检查localStorage的内容,最快的方法是在浏览器中打开开发人员工具 ,转到(JavaScript)控制台选项卡 ,然后键入localStorage。 这应该为您提供如下输出:
- Storage { someKey: “value”, length: 1 } 存储{someKey:“值”,长度:1}
The someKey identifies the value set by JavaScript on the web page you are on. If you believe someKey does something relevant, you can try removing it by running the following:
someKey标识您所在网页上JavaScript设置的值。 如果您相信someKey可以做一些相关的事情,可以尝试通过运行以下命令将其删除:
- localstorage.removeItem(‘someKey’); localstorage.removeItem('someKey');
This removes the data under someKey from localStorage, and reloading the web page may restore it to factory settings. If you are not sure the particular key is set by the cookie notification functionality, you may open the web page in Incognito Mode and list the contents of localStorage before interacting with the web page.
这将从localStorage中删除someKey下的数据,并且重新加载网页可能会将其恢复为出厂设置。 如果不确定cookie通知功能是否设置了特定键,则可以在与网页交互之前以隐身模式打开网页并列出localStorage的内容。
Again, I doubt many websites use localStorage for this purpose.
同样,我怀疑许多网站为此目的使用localStorage。
Have something to add to the explanation? Sound off in the comments. Want to read more answers from other tech-savvy Stack Exchange users? Check out the full discussion thread here.
有什么补充说明吗? 在评论中听起来不错。 是否想从其他精通Stack Exchange的用户那里获得更多答案? 在此处查看完整的讨论线程 。
选择偏好