0

I am currently making a simple list tool. You can click a checkbox to mark it as done and if you navigate away from the page/close the browser and reload, it will still be saved. So it's pretty much just a visual difference.

For this, I'm wondering, should I use PHP or Javascript to set a cookie to save the data on whether or not the item was checked?

If not, should I use $_SESSION? I haven't been able to find a simple solution relevant to my concern, from my searches almost all of them are related to saving usernames/passwords, which of course have an added security aspect too.

Thanks.

3
  • 1
    How 'relevant' is this data? After all a cookie only works in a single browser (and only until the user decides to clean up all cookies). That's not really a place to store data. User views in a different browser and his settings are gone? Similar for session (even worse, since lost when browser closed). This sounds like information that should be persistently stored in a database. Commented Feb 14, 2014 at 9:56
  • For all ends and purposes, for my objective, that's actually OK. Commented Feb 14, 2014 at 9:59
  • How persistent you want this settings/data? Saving it in the database isn't the best choice though. IMHO.
    – Craftein
    Commented Feb 14, 2014 at 10:05

1 Answer 1

1

For the choice there are multiple questions to be asked. How "relevant"is the data, is it shared across browsers, howl on should it live etc.

Cookies are bound to a specific browser. The lifetime can be set from "during this browser session" to some arbitrary date in future. This doesn't mean the lifetime will be reached as there are different events at which a browser might kill the cookies. If its only you and your browser this might work .. ut the list might suddenly be lost.

PHP Session storage is stored on a server and referenced by a cookie or URL parameter. It has a relatively short lifetime (by default 24 hours) as by default the session files reside in the /tmp directory a cleanup process (ie. on server reboot after power failure or update) might delete them. Still not persistent. (PHP session handling could be configured in a quite persistent way but that's ignoring the purpose)

So both probably aren't good. Alternatives? Yes there are!

HTML 5 introduces browser local storage for more data. That is useful to have nicer access to the data and more flexibility in usage. Downside is that this still depends on using the same browser all the time and somecleanup miht free it unexpectedly.

A more robust solution is to use some server-side (PHP) script which stores the list in a database (or simple file) in a place where it is unaffected from cleanup tasks. By that the list can be access from different browsers (i.e desktop and mobile device) and won't easily go away.

1
  • Ideally I don't want it to require any additional server-side handling. I shall look up the browser local storage, that seems rather interesting. If this doesn't work out, I guess I'll have to resort to using cookies. In any case, thanks for pointing me in the right direction! Commented Feb 14, 2014 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.