2

I would like to create a tiny script for web visitors :

A single sentence saying "Welcome {your name}." By cliking on {your name} the user would be able to write ... his name. I can do that by myself.

My question is : What is the best way for the user to see his name the next time he comes back ?

How to store this data in local user storage ?

Thanks for your advice !

Lbh
  • 67
  • 1
  • 7
  • I believe you've answered the question yourself. One option is to use [localStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage) – csum Jun 29 '17 at 17:56
  • Indeed, localStorage seems very interesting ! Do you recommend me to use it instead of cookies ? which of these two methods is the cleanest ? – Lbh Jun 29 '17 at 18:06
  • Cookies are not recommended when you intend to store large amount of information (for that use `localStorage`), though in this case it is perfectly viable to store just the name of the user int a cookie. – lealceldeiro Jun 29 '17 at 18:19
  • Thank you for your help, as a student, i will try both ! – Lbh Jun 29 '17 at 18:25

2 Answers2

1

As you will save just a simple and non-sensible data, do it using cookies. It's easy and will make your job done. cookies() PHP docs

moreirapontocom
  • 458
  • 3
  • 10
1

If you want to store the name in the local storage of the user web browser you can use localStorage (just simple JavaScript). Something like this:

html

<input id='name'>
<a onclick="saveName()">Save</a>
<a onclick="showName()">Show Name</a>

js

function saveName() {
  var name = document.getElementById('name').value;

  if (typeof(Storage) !== "undefined" && name) {
    localStorage.setItem('userName', name);
  } else {
    alert('Sorry! No Web Storage support..');
  }
}

function showName() {
  if (typeof(Storage) !== "undefined") {
    alert(localStorage.getItem('userName'));
  } else {
    alert('Sorry! No Web Storage support..');
  }
}

If you want the user to be able to access his/her info independently of the browser you should consider storing it in the database.

lealceldeiro
  • 14,342
  • 6
  • 49
  • 80