最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

When should I use PHP Session vs Browser Local Storage vs JavaScript Object Parameters? - Stack Overflow

programmeradmin4浏览0评论

When is it appropriate to use the many different ways that modern day AJAX based applications are storing data? I'm hoping for some specific guidelines that I can give developers. Here's what I'm seeing so far, and it's getting messy.

PHP Server Side Session: PHP Session data is probably the oldest way to store session based information. I'm often passing in parameters through various AJAX calls from JavaScript/jQuery objects - to store in PHP Session. I'm also returning objects of data (some session information) back through as a response/result to JavaScript/jQuery methods.

Browser based Local Storage: This is often used to store data that needs to persist on the front end, yet I'm uncertain at times when to use it. One good use was to store geolocation information from navigator.geolocation. I've been storing a lot of information here, but I am not certain that is wise. It never seems to expire, but can be deleted from Resources.

JavaScript Object with config parameter(s): I've been building JavaScipts objects with an init method that sets-up a 'settings' parameter. This is very useful as I usually build it from data passed in from PHP. With jQuery Mobile this data can even persist from page to page and change with AJAX request responses.

So, what guidelines would you give on usage of each?

When is it appropriate to use the many different ways that modern day AJAX based applications are storing data? I'm hoping for some specific guidelines that I can give developers. Here's what I'm seeing so far, and it's getting messy.

PHP Server Side Session: PHP Session data is probably the oldest way to store session based information. I'm often passing in parameters through various AJAX calls from JavaScript/jQuery objects - to store in PHP Session. I'm also returning objects of data (some session information) back through as a response/result to JavaScript/jQuery methods.

Browser based Local Storage: This is often used to store data that needs to persist on the front end, yet I'm uncertain at times when to use it. One good use was to store geolocation information from navigator.geolocation. I've been storing a lot of information here, but I am not certain that is wise. It never seems to expire, but can be deleted from Resources.

JavaScript Object with config parameter(s): I've been building JavaScipts objects with an init method that sets-up a 'settings' parameter. This is very useful as I usually build it from data passed in from PHP. With jQuery Mobile this data can even persist from page to page and change with AJAX request responses.

So, what guidelines would you give on usage of each?

Share Improve this question edited Jul 7, 2014 at 12:13 jjwdesign asked Jun 26, 2014 at 19:14 jjwdesignjjwdesign 3,3229 gold badges42 silver badges67 bronze badges 2
  • 7 If you want it on the server or it has to do with security, use session. If you want it on the client-side and to persist when the user is no longer even online, use browser-based local storage. – developerwjk Commented Jun 26, 2014 at 19:22
  • Third option. This depends on source of config parameters, thus this method operates on different level pared to previous ones and is not an option to choose in between. – mip Commented Jul 7, 2014 at 13:57
Add a ment  | 

1 Answer 1

Reset to default 13

PHP Session data is NOT Permanent Data storage as when you destroy the browsers session you will loose the Data. This is useful if you dont want to permanently store data.

Browsers Local Storage is Permanent unless you delete the data yourself or you clear the browsers cache. Some users clear the cache from time to time so this can be a problem.

Any other method Such as Objects is not permanent Data storage.

Other Browser related Permanent storage are COOKIES (if you don't expire them at session close), IndexedDb (Check here for current browser support http://caniuse./#feat=indexeddb).

So depending on your Website or App you need to decide what data needs to be stored for a short time, or for long time or forever until you deleted it manually.

As an example, you will use LocalStorage if you were storing Bookmarks, and if you were Storing Geolocation points you use Cookies and expire them after you close the browser or the App.

If you were Logging in to an Account using PHP then best practice is to create a PHP Session, and even change the session timeout when the user clicks (Remember me).

These are just a couple of examples from thousands of possible needs.

发布评论

评论列表(0)

  1. 暂无评论