Ok, Here is what I want to do. I have a very complex database. I am reading from the database using php and store the data in a variable (Ajax). Now when I go to another page I loose the variable.
Here is what I know (not sure how correct I am): the scope of a variable is the function it is in. If not specified the scope will be the window object (which changes for every page). Is there any higher level than window? (something like session in php but in the client side)
There are many way to storage your variable such as:
Use client cookie
Use server session or cookie
HTML5 storage
Query string such as ?var1=blah&var2=blah
Maybe you can use cookie. Actually php sessions also utilize cookie as well. As far as I know the scope beyong window is cookie.
Or you can use some HTML5 technologies, such as WebStorage, but that may break on some old browsers.
Normally I would avoid storing state in javascript and instead cache the result on the server. Some sort of application or user level cache might be recommended in this case, so that the other page may benefit from the db result. If it's per user then I would look into session. Otherwise it might be better to store it at the application level in some sort of cache
Related
I have an application in which everything is accomplished, preferably, on the client side. JavaScript on the client side will determine the country location of the user using geolocation. Afterwards, however, the client application needs to perform calculations based upon what country the user is from. I have a list of variables for each country - UK:176 - for example - how should I deliver these variables to the client?
One thing crossing my mind was to store the data in hidden html tables, that the JavaScript could fetch based upon country later. However, that solution is very hacky. And obviously having some 200+ JS variables would harm performance. Is local web storage a good alternative, for example?
If you have to store the variables on the client side, yes, then local web storage is probably the best way to go. If you don't need the data persistent you could also store the variables in the session storage. As the name implies, this store is lost if the user closes the session (closing the tab or browser).
I am developing a simple login system, and I'll have to store two cookies. One indicating which user is logged, and other indicating the "plan" (database) that the user belongs. Well, at client side the first cookie (us_auth) and the second (pl_auth), will store hashes (md5) to make a key that I can check on server side.
Now at the server side I'll store these two keys to assign to them values to route the user correctly. A simple authentication system.
Well, here the question comes. At PHP we have $_SESSION, and at Node we have modules that make the same thing, but I want something that I have more control, to work directly with the data. I have two choices:
Memcached, or a simple global var that store an object with key->value (in my case, hash->value).
What's the best choice in my case? At simple point of view, the second alternative seems to be more fast, pratical and simple.
A simple global variable will fail if you replicate your application. Variables are not shared between processes (or even machines).
If you don’t plan on replicating your app, just restart your process, the memory allocated to that process will be lost, so will your in-memory session variables.
A memcached server can help you with that.
This is not specific to nodejs. You’ll have the same problem with PHP or any other technology.
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?
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.
At first i am not sure if using cookies is the right way.
What i basically need is a variable that i store from one page , and then to use it on another page.
If it can be done without cookies then its great , if not then i will just have to do it with cookies.
For setting cookies within JavaScript have a look at here.
Remember, that the data you store in cookies is transmitted with every request for that page or domain. So if you want to store larger amounts of data, maybe local storage is more suitable.
I'm working on a web based form builder that uses a mix of Jquery and PHP server side interaction. While the user is building the form I'm trying to determine the best method to store each of one of the form items before all the data is sent to the server. I've looked at the following methods
Javascript arrays
XML document
Send each form item to the server side to be stored in a session
The good, the bad and the ugly
Depends on your application functionality and requirements, but Javascript would probably be the best way. You can use either arrays or objects or whatever in javascript. It's server independent and it will preserve data over a long period of time as long as client session stays present (browser window doesn't close for whatever reason) but this can be quite easily avoided (check my last paragraph).
Using XML documents would be the worst solution because XML is not as well supported on the client side as you might think.
Server side sessions are good and bad. They are fine if you store intermediate results from time to time, so if client session ends because of whatever reason, user doesn't loose all data. But the problem is that it may as well expire on the server.
If I was you, I'd use Javascript storage and if needed occasionally send JSON serialized results to server and persist them there as well (based on business process storig this data somewhere else than session could be a better solution). I'd do the second part (with sever side combination) only if I would know that user will most probably build forms in multiple stages over a longer period of time and multiple client sessions. but can be used for failure preventions as well. Anyway. Javascript is your best bet with possible server-side interaction.
Preserving data between pages on the client
Be aware that it's also possible to preseve data between pages on the client side. Check sessvars library for this. So even if the page gets refreshed or redirected and then returned all this can be stored on the client side between these events like magic. Marvelous any rather tiny library that made my life several times. And lessened application complexity considerably that would otherwise have to be implemented with something more complex.
I used TaffyDB to store data, and it's just wonderfully easy to implement.
Hope this helps you
You may want to check out PersistJS, which exposes a cross-browser persistent storage object. Of course, being persistent, data stored with this library survives sessions, not just page changes.
The latest version (0.2.0) is here – note the version in the above linked post is 0.1.0.
A combination of #1 (although I'd use objects, not arrays necessarily) and #3 would seem like a good approach. Storing the data locally in the browser (#1) makes it immediately accessible. Backing that up with session-based server-side storage defends you from the page being refreshed; you can magically restore the page just as it was.