What's the best way to generate PHP or Javascript cookies?

  • 0
    I decided to make a small online store, since it is small, I see no reason to make a registration system on the site. I plan to create a "Wishlist on the site", and store the products that the user has saved in cookies, in this regard, the question arose how best to generate cookies using PHP or Javascript? Will this somehow affect the safety or speed of work, depending on the chosen method?
    JavaScript Anonymous, Jan 14, 2020

  • 2 Answers
  • 0
    Personally, I prefer not to store data in Cookie or even in localStorage, but in the built-in database IndexedDB .



    We include a special library that simplifies working with the InxdexedDB base:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/localforage/1.9.0/localforage.js"></script>


    Next, to save some data, we execute in the browser console:

    result = await localforage.setItem('key', save_data);
    console.log('Данные успешно сохранены:');
    console.dir(result);


    Where in save_data is the saved data, and key is the key by which you could get access to them.



    One of the important features here is that in save_data we can put any data type - even a string, even an array, or an object. You can even save the file if you want! At the same time, there are no restrictions on the amount of stored data, you can shove at least a megabyte or even a hundred megabytes - the built-in storage will gobble up everything.



    To later retrieve the saved data, just execute:

    result = await localforage.getItem('key');
    console.log('Данные успешно извлечены:');
    console.dir(result);


    Data storage period is unlimited. Or until the user manually zeroes the browser.
    Anonymous

  • 0
    Depending on the situation.



    If I need to do some preliminary check, then I work on the backend side.



    If no checks need to be done, then I add it from the client side.
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!