Other Alternatives to Cookies - javascript

I am using cookies to store data at client end for later access. But my concern is I am unable to set more than 20 cookies and I am looking for alternative to cookies.
Please help me on this
Updated
I found jStorage plugin here But it does not work for me in this case..

You can leverage local/session storage of HTML5
To save a value:
localStorage.name = "Bob";
To get a value:
alert(localStorage.name);
http://www.w3.org/TR/webstorage/

The two main options are Web Storage and Web SQL Database.

Are you storing one piece of information in each cookie? Because you could use JSON serialization to store more data in each individual cookie.

There are few alternatives to cookies
Session (server side)
If HTML5 compliant browser then you can even have client side database

You can store only one cookie representing a session ID (for example, an alfanumeric randomly generated long string). Then you just need a database to store all your data (that now is on 20 cookies) together with that session ID. At runtime, you read from the only cookie the session ID, and load from the DB all data.

Related

Is it possible to view secret variable declared in angular in browser, after build and run?

I want to use an a decryption algorithm on the data I receive through REST-API, for that I am storing the secret-key in angular component ts file.
I want to know that after I build and deploy the code. Will my secret key be visible somewhere in the browser, because the built code is pure javascript which is fetched by server.
I am using crypto-js library for decryption of data.
var secret_key = "super-secret";
CryptoJS.AES.decrypt(ciphertext, secret_key)
If you can explain the situation why you need it, may be a chance you can get better way to do it.
There are 3 different ways to achieve it :-
Pass roles/permissions to local storage as well as same time put into session table(or any other table) in backend. Check role/permission every time user request for something to make sure it is not modified.
Store the roles/permission in backend e.g in session table and then provide roles/permission on every request. This way you do not have to worry about any modification by user anytime.
Pass the encrypted (RSA algorithm)roles/permission in cookies and make cookie httpOnly true and secure true, these cookies cannot be accessed/modified as per protocol and then in each request you will have
The best practice here is to execute encrypt/decrypt operation on backend (server-side) and get the decrypted value from you API
Yes it will be visible to individuals looking for it. You should never store secrets in any client-side code.
The key should be acquired from a server once the client authenticates.
Good post on key storage options
https://pomcor.com/2017/06/02/keys-in-browser/
Section 5.2 of WebCryptoAPI discusses a bit about key storage
https://www.w3.org/TR/WebCryptoAPI/

How can I check using PHP if some cookie is created through javascript?

I want my PHP code to differentiate between cookies created by JavaScript (using document.cookie) and PHP (using setcookie()).
Let us suppose below pseudocode:-
<?php
$X = $_COOKIE['X'];
if($X_COOKIE_IS_SET_BY_PHP)
// ALL IS GOOD
else if($X_COOKIE_IS_SET_BY_JAVASCRIPT)
// SET A COOKIE USING PHP
?>
There is no difference. Cookies are cookies.
The only way (though highly unreliable), is to remember in the session whether you set the cookie from PHP. If not, then it might have been set from JavaScript. But like I said, this is unreliable. The cookie might have been set from PHP in an earlier session, or someone might have manually messed with the cookies.
I think the best way is to use session variables altogether. The session keeps the leading value on the server. If you need the value in JavaScript too, you can set a cookie, or add the value in a small script, but in PHP you never read back the cookie value at all, just use the session value on the server, and to reset the cookie on each request, so the client knows that value too.
Cookie isn't something stored in server side. It's a piece of data stored in the user's computer / browser. When you use setcookie() in PHP, The server sends the cookie with header which the browser stores in the user's computer (and the browser sends it back with next requests). The place that cookie is stored is the same place where document.cookie would store. Hence, after it's stored, we cannot differentiate it whether it was stored by PHP or Javascript, because no information regarding that is recorded.

Cookies for different user in one browser

There is some drop down on the page of web site (user has access to this page only if he/she is authenticated) an I want to save this value to cookies and set it back to drop down when user gets back to my site.
It is not a problem to save currently selected drop down option value to cookie and retrieve it later. But I faced with some problem if I make login at the same browser by another user. It gets from cookies value what was saved by previous user.
So what is good way to separate cookies for different users from the same browser? I was thinking about create cookie with name like 'username-dropdown' but I have some doubts that it is the best solution.
I use Java with Tomcat 8.
I'm going to take a guess here that you don't need this information sent to your server with each and every HTTP request; you just need to store the information client-side (and you can send it to the server as necessary via ajax).
If so, I'd use local storage, not cookies. And sure, using the username or user ID or some such is reasonable:
// Setting:
localStorage.setItem(username + "-dropdown", value);
// Getting:
var value = localstorage.getItem(username + "-dropdown");
Or you can use brackets notation:
// Setting:
localStorage[username + "-dropdown"] = value;
// Getting:
var value = localstorage[username + "-dropdown"];
Pretty much the only reason not to use brackets notation is if you need to polyfill local storage on older browsers (there are polyfills that fall back to cookies for you), but local storage is supported on all modern browsers, and also IE8, so those browsers really are very out of date at this point.

Store data locally using HTML and JavaScript

I have a small LAN with some computers.
I'm looking for a way to build a dynamic HTML webpage that uses JavaScript to store some data locally (can't use server side - only client side).
The webpage will be stored on a network drive shared with all the computers.
I wish to do that using a file, maybe an XML file or something similar that will be loaded using JavaScript and then saved again after some changes.
The data must be shared with all the computers on the LAN.
How can I do this?
HTML5 localStorage
//Set
localStorage.setItem("lastname", "Smith");
//Get
var lastName = localStorage.getItem("lastname");
You have the following options :
1.LocalStorage : You can store data in variables. There would be a limit as to how much data you can store.
Refer : http://en.wikipedia.org/wiki/Web_storage.
Eg:
// Store
localStorage.setItem("sample", "test");
// Retrieve
var sample = localStorage.getItem("sample");
2.WebSQL : This should be the most easy way of storing in client side. WebSQL is supported in almost all current browsers(HTML5). However there is no longer official support for WebSQL as its depreciated and no future updates.
Refer : http://en.wikipedia.org/wiki/Web_SQL_Database
3.IndexedDB : This is also another way to store data in local database. However this is not yet supported in all browsers.
4.XML
If you are going forward with storing data in local DB, then you can utilize PersistenceJS.
Refer : https://github.com/zefhemel/persistencejs
Try like this:
store a value :
localStorage.setItem("lastname", "amir");
get a value:
localStorage.getItem("lastname");
You can use HTML 5 LocalStorage
Depending on the flavor of data you're storing, simple cookies might work, accessed with plain old JS.
finally I found a solution for it!
I am using a jQuery plugin called: twFile (http://jquery.tiddlywiki.org/twFile.html).
It uses an activeX FileSystemObject - it works great on IE9.

Client side data storage

I have the following situation: the user submits a form, I fetch the data from the server via an Ajax request and displays a chart. However, I want to give the user the option to display the data in the chart in table form or export as csv after he had submitted the form.
I was wondering what's the best solution to store the data, considering that I don't want the data to persist if the user opens a new window to submit the form again for example.
The application is in Rails.
Thanks.
You have a few options:
Cookies
LocalStorage
SessionStorage
More info: https://developer.mozilla.org/en/DOM/Storage
Non-standard:
window.name can store anywhere from 1mb up to 10mb depending on the browser. This is more of a hack, but is fairly stable. You would need to implement your own accessor/setter methods on this, where localStorage and sessionStorage have API's built in.
Personally i would recommend local storage if all your users browsers support it.
Its very simple to use and you can access it using these to methods.
localStorage.getItem("Itemkey");
localStorage.setItem("Itemkey","value");
localStorage.removeItem("ItemKey");
Its always a good way to go and this means you can assign each window a differnt local storage key and even remove the item when the window is closed, or unloaded !
For reference I found this very useful: http://diveintohtml5.info/storage.html
And combine it with storing JSON objects ( http://www.json.org/js.html ) and you have a very fast,simple and easy to use solution. OR even just store a string,array or what ever is required.

Categories

Resources