Session handling in HTML page using JavaScript [on hold]


Session handling in HTML page using JavaScript [on hold]



Can we do session handling in HTML page with the help of javascript?



If yes, how? If not, why?



Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.





What do you mean with session handling? You can set and edit cookies if that is what you mean.
– TJHeuvel
May 7 '12 at 9:29





6 Answers
6



javascript only support cookies. You can set them up to keep track of a user session but they do not support the use of sessions.


function createCookie(name,value,days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires="+date.toGMTString();
}
document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1,c.length);
}
if (c.indexOf(nameEQ) == 0) {
return c.substring(nameEQ.length,c.length);
}
}
return null;
}

function eraseCookie(name) {
createCookie(name,"",-1);
}



Source: http://www.daniweb.com/web-development/javascript-dhtml-ajax/threads/19283/how-to-save-session-values-in-javascript



Session is a server side mechanism, so you need a server logic to start and manage sessions.



while the term "session" usually is used for a server side mechanism, that naturally can't be used without some server side script, you could implement a pseudo-session in JS if you only want to have a login tracking:



be careful, though,
1. such a mechanism may cause some stress on your clients browsers depending on your site and parameters (watched actions, frequence of the check interval)
2. this wouldn't survive if the user closes the browser window/tab



Today (September 2017) I would recommend using the HTML5 Web Storage feature.



Quoting the W3C:



HTML web storage; better than cookies.
What is HTML Web Storage?



With web storage, web applications can store data locally within the
user's browser.



Before HTML5, application data had to be stored in cookies, included
in every server request. Web storage is more secure, and large amounts
of data can be stored locally, without affecting website performance.



Unlike cookies, the storage limit is far larger (at least 5MB) and
information is never transferred to the server.



Web storage is per origin (per domain and protocol). All pages, from
one origin, can store and access the same data.



More details about how it works and how to implement it here: https://www.w3schools.com/html/html5_webstorage.asp



Also the MDN Docs are a good source for even more details: https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage



Obviously encrypting the data stored in Web Storage wouldn't hurt.



Sessions can't be accessed by JS directly. It's stored on server, while javascript is running on client. But can be done indirectly, e.g., store it in a hidden filed, submit it to the server, and retrieve and assign the values in hidden filed to session.



Can we do session handling in HTML page with the help of javascript?



Indirectly. Use AJAX to invoke a server-side HTTP handler that is session-aware. jQuery.ajax() makes AJAX easy and there are many examples.


jQuery.ajax()



For example, this is done in .Net by calling a WCF endpoint, a web service, or even a page that has access to the same process in which session state is maintained.



You can both get/set values in session in this manner.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV