What is the best way of storing persistent data ? I want to save the time the user was last active on the page.At first I used localstorage, then i switched to cookies because it seemed easier to use, now I'm thinking of storing it in a database.What do you suggest.
Also could you please suggest a good method of registering when a user left the page 开发者_运维问答?
Depending on how user specific you want that mechanism to be you should either go for a client or server side approach. Local storage is obviously depending on the device, or to be more precise what browser the user is using to access your website.
If your website is likely to be accessed from the same machine/browser every time the user visits your website then cookies should do the job. There is no need for local storage as this is more suitable for bigger chunks of data. Furthermore the user might be using a browser that doesn't support local storage. Cookies have been around for ages and will work on pretty much every browser and device.
If your website is however likely to be accessed from different devices, let's say home computer, office computer and maybe a smartphone too, then you should think about a server-side solution using a user account. But make sure that the user actually benefits from the effort to create a user account (maybe using OpenID like stackoverflow). Otherwise you put the success of your website at risk.
In short ... it's more a design decision, know your users and identify what you're trying to achieve.
Cookies are frequently used for storing non-critical user preferences that refer mostly to website usage, such as auto-login, language, themes, time zone etc. Since these are stored on user's local machine, cookies are client-side technology and can be deleted freely by the client, so you probably would not want to rely on cookies for storing critical data.
However, in case of storing domain-specific data, such as usernames, passwords, emails, favorites etc., you really might want to use a relational database (RDBMS), such as MySQL, MS SQL or PostgreSQL. These are viable on handling massive amounts of data and feature scores of useful features, i.e. transactions, efficiency, flexibility.
If you need to save other info about the users I would say to store this info in the database along with the other information, i.e. you could add a field "last_time" to the users database table. If you don't use a DB instead (very strange), adding one just to store this information may be a bit exaggerate. Could you elaborate a little on HOW you have to use this information? Using cookies allows you to access it only when the user is connected to the site, so I don't think it is the best solution.
Overall, anyway, I think SQL is the best choice.
精彩评论