是否可以在 JavaScript 中创建会话变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5818917/
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-23 19:03:14  来源:igfitidea点击:

Is it possible to a create session variable in JavaScript?

javascript

提问by saint

Is it possible to create and update a session variable in JavaScript? Just like in PHP as I start sessions and initialize session variable.

是否可以在 JavaScript 中创建和更新会话变量?就像在 PHP 中一样,我开始会话并初始化会话变量。

I'm using some JavaScript function with PHP.

我在 PHP 中使用了一些 JavaScript 函数。

I'm just a beginner in JavaScript so please also refer me some good books.

我只是 JavaScript 的初学者,所以也请给我推荐一些好书。

回答by Quentin

The usual meaning of the term "Session variable" is: "Some data stored on the server, which is associated with a user's session via a token".

术语“会话变量”的通常含义是:“存储在服务器上的一些数据,通过令牌与用户的会话相关联”。

Assuming you are talking about client side JavaScript, then the short answer is "no" as it runs on the wrong computer.

假设您在谈论客户端 JavaScript,那么简短的回答是“不”,因为它在错误的计算机上运行。

You could use JS to make an HTTP request to the server, and have a server side programme store data in a session variable.

您可以使用 JS 向服务器发出 HTTP 请求,并让服务器端程序将数据存储在会话变量中。

You could also use JS to store data in a cookie (and not set an expiry time so it expires at the end of the browser session)

您还可以使用 JS 将数据存储在 cookie 中(并且不设置到期时间,因此它会在浏览器会话结束时到期)

回答by mattsven

Well, taking a look at how sessions work, no, they cannot be created with javascript. You can, though, make an AJAX request to your PHP file to set one.

好吧,看看会话是如何工作的,不,它们不能用 javascript 创建。但是,您可以向 PHP 文件发出 AJAX 请求以设置一个。

PHP:

PHP:

<?php
session_start(); 
$_SESSION['mySession'] = 1;
?>

JS:

JS:

var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "session_maker.php", true);

xmlhttp.onreadystatechange = function(){
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
        alert("Done! Session created.");
    }
};

回答by James

There are a number of ways of recording state in Javascript - none of which are particularly great / cross-browser, but there you go.

在 Javascript 中有多种记录状态的方法 - 没有一种特别好/跨浏览器,但是你去。

You can set and get cookies - http://www.quirksmode.org/js/cookies.html

您可以设置和获取 cookie - http://www.quirksmode.org/js/cookies.html

Window.name storage (sessionvars) - http://www.thomasfrank.se/sessionvars.html- this is a very cool hack, but probably not a great idea to use for anything important

Window.name 存储 (sessionvars) - http://www.thomasfrank.se/sessionvars.html- 这是一个非常酷的 hack,但对于任何重要的事情来说可能不是一个好主意

HTML5 local storage - http://diveintohtml5.ep.io/storage.html

HTML5 本地存储 - http://diveintohtml5.ep.io/storage.html

Server-side is probably the way to go here, but client-side you have a few options.

服务器端可能是这里的方式,但客户端你有几个选择。

回答by Daniel A. White

No, session state is server-side. You can, however, create cookies that PHP can read.

不,会话状态是服务器端。但是,您可以创建 PHP 可以读取的 cookie。

回答by Rex

You could use the js via the ajax way from/to load/post to a php script and write your session control method into this php script file.

您可以通过 ajax 方式使用 js 从/加载/发布到 php 脚本并将会话控制方法写入此 php 脚本文件。

You could try the jQuery's $.ajax() method, I guess it is the easy way to made it on the front-end script.

您可以尝试 jQuery 的 $.ajax() 方法,我想这是在前端脚本上制作它的简单方法。