使用 JavaScript 在 HTML 页面中处理会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10479676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:32:14  来源:igfitidea点击:

Session handling in HTML page using JavaScript

javascripthtmlsession

提问by user1379384

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

我们可以在 javascript 的帮助下在 HTML 页面中进行会话处理吗?

If yes, how? If not, why?

如果是,如何?如果不是,为什么?

回答by Murtaza

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.

javascript 仅支持 cookie。您可以将它们设置为跟踪用户会话,但它们不支持使用会话。

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

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

回答by yuvin

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

Session 是一种服务器端机制,因此您需要一个服务器逻辑来启动和管理会话。

回答by cluxter

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

今天(2017 年 9 月)我建议使用 HTML5 Web Storage 功能。

Quoting the W3C:

引用 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.

HTML 网络存储;比饼干好。什么是 HTML 网络存储?

通过网络存储,网络应用程序可以在用户浏览器中本地存储数据。

在 HTML5 之前,应用程序数据必须存储在 cookie 中,包含在每个服务器请求中。Web存储更安全,可以在本地存储大量数据,不影响网站性能。

与 cookie 不同的是,存储限制要大得多(至少 5MB)并且信息永远不会传输到服务器。

Web 存储是每个源(每个域和协议)。来自一个源的所有页面都可以存储和访问相同的数据。

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

有关它的工作原理以及如何在此处实施的更多详细信息: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

此外,MDN 文档也是获取更多详细信息的好来源:https: //developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage

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

显然,加密存储在 Web Storage 中的数据不会有什么坏处。

回答by cypherabe

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:

虽然术语“会话”通常用于服务器端机制,如果没有一些服务器端脚本自然不能使用,如果您只想进行登录跟踪,则可以在 JS 中实现伪会话:

  • implement a hidden field with the timestamp of the user login as value
  • update this field with the new timestamp each time the user makes a significant action
  • run a pseudo-cronjob with setinterval(), check if the value in the hidden field ist older than your allowed session time
    • if it's older, execute your session timeout code
  • 实现一个隐藏字段,以用户登录的时间戳作为值
  • 每次用户进行重要操作时使用新时间戳更新此字段
  • 使用 setinterval() 运行伪 cronjob,检查隐藏字段中的值是否比您允许的会话时间早
    • 如果它更旧,请执行您的会话超时代码

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

但是要小心,1. 根据您的站点和参数(监视的操作,检查间隔的频率),这种机制可能会对您的客户端浏览器造成一些压力 2. 如果用户关闭浏览器窗口/选项卡,这将无法生存

回答by swapnesh

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.

JS 不能直接访问会话。它存储在服务器上,而 javascript 在客户端上运行。但是可以间接完成,例如,将其存储在隐藏字段中,将其提交给服务器,并检索隐藏字段中的值并将其分配给会话。

回答by Tim Medora

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

我们可以在 javascript 的帮助下在 HTML 页面中进行会话处理吗?

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

间接地。使用 AJAX 调用可识别会话的服务器端 HTTP 处理程序。jQuery.ajax()使 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.

例如,这是在 .Net 中通过调用 WCF 端点、Web 服务或什至可以访问维护会话状态的同一进程的页面来完成的。

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

您可以通过这种方式在会话中获取/设置值。