LocalStorage 替代 cookie?

发布于 2024-11-26 00:51:41 字数 315 浏览 2 评论 0原文

我听到有人说本地存储将在几年内取代 cookie。

这种说法对我来说听起来有点错误,原因如下: 使用 PHP(或任何服务器端语言),我们可以访问 cookie,但我还没有找到访问 LocalStorage 的方法。

使用 Ajax 不是解决方案: Ajax 不是一个好的解决方案,因为它意味着我必须在访问本地存储之前显示一个页面。在许多情况下,这是不可接受的。考虑存储站点的显示语言,您必须以随机(默认)语言显示一个页面一次,然后站点将切换到第二个页面。

我们是否应该考虑仅针对客户端数据进行本地存储,而不考虑服务器端(数据库等)?

I've heard some people saying that local storage would replace cookies in a couple of years.

That statement sound a bit false to me for one reason:
With PHP (or any server side language), we can access cookies, but I've not yet found anyway to access LocalStorage.

Using Ajax is not a solution:
Ajax is not a good solution as it mean I must display a page before having access to the Local Storage. In many case, that's unacceptable. Think of storing the display language of a site, you would have to display a page once in a random(default) language, and then the site would switch for the second page.

Should we consider local storage for and only for client-side data, without any incidence on the server side ( database, etc.. ) ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

送舟行 2024-12-03 00:51:41

是的,顾名思义,LocalStorage 的目的是在客户端本地存储数据。它扮演着与 cookie 不同的角色。
您可以在其中存储更多数据,但该数据不会发送到服务器(每个连接发送 1Mb 的数据会很糟糕)

Yes, as its name implies, LocalStorage is aimed at storing data locally, at the client. It fills a different role than cookies.
You can store much more data in it, but that data isn't sent to the server (it would be horrible to send 1Mb of data with each connection)

千里故人稀 2024-12-03 00:51:41

我认为从 cookie 切换到 html5 本地存储没有任何意义,除非该网站设计为在互联网中断时运行。但这只是我的意见

i don't think there would be any point in switching from cookies to html5 local storage unless the site was designed to run when the internet was down. but thats just my opinion

海未深 2024-12-03 00:51:41

在初始获取请求期间,将发送该域的所有预先存在的 cookie,并且服务器可以确定以前的用户。我不明白如何使用 localStorage 来完成相同的功能。 LocalStorage 使用 Javawcript 读取和设置。您不能在初始请求中使用 Javascript。我看到两者都被使用,而不是 LocalStorage 取代 cookie。

During the initial get request, all pre-existing cookies for the domain is sent and the server can determine a previous user. I don't see how the same functionality is done using localStorage. LocalStorage is read and set using Javawcript. You can't use Javascript in the initial request. I see both being used, not LocalStorage replacing cookies.

唠甜嗑 2024-12-03 00:51:41

关于浏览器存储存在一些混乱。来自 DOM 存储指南
“DOM 存储旨在提供更大、更安全且更易于使用的替代方案,以替代在 Cookie 中存储信息。”

现在,本地存储确实是为了本地存储而创建的。例如,您可以将之前应用的过滤器存储在网格上。或者用户偏好。这就是它所属的地方。欧盟要求 cookie 用户同意的确切原因是服务器通常会保留太多他们不需要的(个人)信息。

如上所述,还有会话存储,它是临时的并且在刷新之间持续存在。

不过,服务器上的内容由您决定。当用户从另一个地址登录时,上述首选项可以保存在服务器上。但是将它们保存在本地存储中就像缓存一样,因此您不需要每次都请求它们。

Cookie 对于会话维护仍然有用,因为您可以在每个请求中发送一个标识符。

PS:我不明白为什么“加载前显示页面”是一个问题。在获得数据之前,您始终可以有一个加载动画。

There is some confusion regarding browser storage. From the DOM Storage Guide:
"DOM Storage is designed to provide a larger, more secure, and easier-to-use alternative to storing information in cookies."

Now, local storage is indeed created for local storage. For instance, there you could store previously applied filters on a grid. Or user preferences. This is where it belongs. The precise reason why the EU required cookie user consent was that often servers keep too much (personal) information that they do not need.

As mentioned above, there is also session storage, which is temporary and persists between refreshes.

It is up to you to decide what belongs on the server, though. The above-mentioned preferences could be saved on the server for when the user logs from another address. But keeping them in local storage acts like a cache, so you don't need to request them each time.

Cookies remain useful for session maintenance, because you can send an identifier with each request.

P. S.: I don't understand why 'showing the page before loading' is a problem. You could always have a loading animation until you get the data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文