开发者

LocalStorage to replace cookie?

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 开发者_StackOverflow中文版consider local storage for and only for client-side data, without any incidence on the server side ( database, etc.. ) ?


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)


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜