Handling Sessions on an HTML Page Using JavaScript

Is it possible to handle a session on an HTML page using javascript?

If so, how? If not, why?

+6
source share
6 answers

javascript only supports cookies. You can configure them to track 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

+5
source

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

+2
source

while the term โ€œsessionโ€ is usually used for a server-side mechanism that naturally cannot be used without any server side script, you could implement a pseudo session in JS if you only want to have login tracking:

  • implement a hidden field with user login timestamp as value
  • update this field with a new timestamp every time the user takes a significant action.
  • run the pseudo-cronjob with setinterval (), check if the value in the hidden field is older than your allowed session time
    • if he is older, complete your session timeout

be careful, 1. such a mechanism can lead to some stress in the browsers of your clients depending on your site and parameters (observed actions, frequency of the test interval) 2. this will not be saved if the user closes the browser window / tab

+2
source

Sessions cannot directly access JS. It is stored on the server, and javascript runs on the client. But it can be done indirectly, for example, by storing it in a hidden folder, sending it to the server and retrieving and assigning values โ€‹โ€‹to the hidden record for the session.

+1
source

Is it possible to handle sessions on an HTML page using javascript?

Indirectly. Use AJAX to call the HTTP handler on the server side that supports the session. jQuery.ajax() simplifies AJAX and there are many examples.

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

You can both get / set values โ€‹โ€‹in a session this way.

+1
source

Today (September 2017), I would recommend using the HTML5 web storage feature.

Quoting W3C:

HTML web storage better than cookies. What is HTML web storage?

With web storage, web applications can store data locally within a user browser.

Prior to HTML5, application data must be stored in cookies, including in each 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 much larger (at least 5 MB) and information is never transmitted to the server.

Web page repository for each source (for domain and protocol). All pages, from one source, can store and access the same data.

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

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

Obviously, encryption of data stored in Web Storage will not hurt.

+1
source

Source: https://habr.com/ru/post/915070/


All Articles