Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 802
jjwdesign Avatar asked Jun 26 '14 19:06

jjwdesign


People also ask

When should I use local storage vs session storage?

sessionStorage is similar to localStorage ; the difference is that while data in localStorage doesn't expire, data in sessionStorage is cleared when the page session ends. Whenever a document is loaded in a particular tab in the browser, a unique page session gets created and assigned to that particular tab.

When should I use browser session storage?

Session storage is a popular choice when it comes to storing data on a browser. It enables developers to save and retrieve different values. Unlike local storage, session storage only keeps data for a particular session. The data is cleared once the user closes the browser window.

When should you use PHP sessions?

Sessions are a simple way to store data for individual users against a unique session ID. This can be used to persist state information between page requests. Session IDs are normally sent to the browser via session cookies and the ID is used to retrieve existing session data.

What is the main purpose of using PHP session storage?

A session is a way to store information (in variables) to be used across multiple pages. Unlike a cookie, the information is not stored on the users computer.


1 Answers

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.com/#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.

like image 168
Tasos Avatar answered Oct 01 '22 00:10

Tasos