-2

I would like to enable a user to save their preferences without having to log in. I was thinking of using the user's IP to save their preferences, but this doesn't work for 'workplaces' where multiple people will be on the same IP-connection.

What would be the best way to do this? Basically, I want to store a per-person session without using a database (and hopefully, without having to use a secondary data-store, such as redis).

Is it possible to do this in javascript?

David542
  • 104,438
  • 178
  • 489
  • 842
  • 1
    [`localStorage`](https://developer.mozilla.org/en-US/docs/Web/API/Storage/LocalStorage)? – Evan Davis Aug 20 '15 at 17:18
  • @Mathletics what's the difference between a javascript `cookie` and a javascript `localStorage` ? – David542 Aug 20 '15 at 17:20
  • 1
    @David542 You know you can just Google it? Basically localStorage is HTML5 web storage, it's more powerful and has more space compared to cookies. – Spencer Wieczorek Aug 20 '15 at 17:23
  • The available size (though I don't know the limits off the top of my head), and `Storage` objects can take multiple key:value pairs vs cookies (I think) just store strings that you have to parse/stringify. – Evan Davis Aug 20 '15 at 17:29

4 Answers4

1

Cookies are your best bet. Tied to the browser of a user, and available for your server too - https://developer.mozilla.org/en-US/docs/Web/API/document/cookie

You may also look at local storage - https://developer.mozilla.org/en/docs/Web/API/Window/localStorage

Understand the difference between the two and make a choice - Local Storage vs Cookies

Community
  • 1
  • 1
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • thanks for this answer. Could you show a very simple implementation of how this would work in javascript? And could you please explain what you mean by `local storage` ? Is this an html5 thing, or something like memcache? – David542 Aug 20 '15 at 17:18
  • 1
    @David542 - added relevant docs. It's time you do some searching and reading as well. – manojlds Aug 20 '15 at 17:22
1

From your description, I think that saving these preferences on the user's machine is the best option. If it is small amount of data (less than 100KB), then use cookies. If it is big (but still within allowed limits approx 5MB) then use local storage (indexedDB or webStorage).

maaeab
  • 326
  • 1
  • 7
1

You can use localstorage. You can store key-value items.

Set value:

window.localstorage.setItem(key, value);

Get value:

var value = window.localstorage.getItem(key);

Example: http://jsfiddle.net/wasnvvo7/1/

Misaz
  • 3,694
  • 2
  • 26
  • 42
0

Just give the user a unique key to store as a cookie and then use that key to keep track of that user's data. --WARNING!! if the user moves to a different machine or deletes his/her cookies, the session will not be usable.

Silvertail
  • 169
  • 1
  • 13