javascript 使用 jQuery 设置会话变量(不通过 Post 或 Get 传递)

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

Setting session variable with jQuery (without passing via Post or Get)

phpjavascriptjquerysession-variables

提问by Phas1c

So I have a page where I'm requesting users to enter a key via an <input>tag. I would like to store this information in $_SESSION['sessionKey']when the user clicks a button. Since I'm trying to keep the key moderately secure, I want to avoid passing this information via GET or POST.

所以我有一个页面,我要求用户通过<input>标签输入密钥。我想$_SESSION['sessionKey']在用户单击按钮时存储此信息。由于我试图保持密钥适度安全,因此我想避免通过 GET 或 POST 传递此信息

Most of what I've found online shows this done by using GET/POST and I'm having difficulty finding information on a method that would not use this approach. I did find this question, which suggests to put JavaScript in a file that has an extension of .php, and from there, use the PHP tags to obtain the $_SESSION variables. I followed this approach like so...

我在网上找到的大部分内容都显示这是通过使用 GET/POST 完成的,而且我很难找到有关不会使用这种方法的方法的信息。我确实发现了这个问题,它建议将 JavaScript 放在一个扩展名为 .php 的文件中,然后使用 PHP 标签获取 $_SESSION 变量。我像这样遵循这种方法......

javascript.php

javascript.php

<?php
require ("common/startandvalidate.php");
?>

$(document).ready(function() {
    $("#submitButton").click(function(){
        <?php $_SESSION['sessionKey']?> = $("#sessionKeyInput").value;
    });
});

mainPage.php

主页.php

<head>
   <script src="javascript.php"></script>
   <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>

<input id="sessionKeyInput" placeholder="Session Key" />
<input id="submitButton" value="Submit" type="button" />

When I look at the page through Inspector (in Chrome) I see the error message:

当我通过 Inspector(在 Chrome 中)查看页面时,我看到了错误消息:

Undefined index: sessionKey

the require("common/startandvalidate.php");contains session_start(), so the session is being initiated...

require("common/startandvalidate.php");包含session_start(),所以会被启动...

I think it's not working because $_SESSION['sessionKey']has never been declared before, so even though I want to assign a value to it, it's trying to see the variable contents, which is undefined. How could I go about assigning this value to the $_SESSION variable?

我认为它不起作用,因为$_SESSION['sessionKey']以前从未声明过,所以即使我想为其赋值,它也试图查看未定义的变量内容。我怎样才能将此值分配给 $_SESSION 变量?

Sorry if this is a simple question, any resources are appreciated!

对不起,如果这是一个简单的问题,任何资源都将不胜感激!

回答by Jason P

Javascript runs on the client and doesn't have access to the server, and therefore the session. If you want to put a user-entered value in the session, you need to pass it to the server. The most secure way to do that is an SSL-protected HTTP POST.

Javascript 在客户端上运行,无法访问服务器,因此无法访问会话。如果要将用户输入的值放入会话中,则需要将其传递给服务器。最安全的方法是使用 SSL 保护的 HTTP POST。

回答by Drahcir

$_SESSIONis a PHP array, you cannot set it on the client side via Javascript. The value has to be sent to the server to be stored in the array.

$_SESSION是一个 PHP 数组,您不能通过 Javascript 在客户端设置它。该值必须发送到服务器以存储在数组中。

The question you pointed toshows how you can retrieve the data from $_SESSION, but it won't work for storing data.

您指出的问题显示了如何从中检索数据$_SESSION,但它不适用于存储数据。

The error you see in the console "Undefined index: sessionKey" simply means that the Javascript array named $_SESSIONhas no key named "sessionKey".

您在控制台中看到的错误“未定义索引:sessionKey”仅表示名为 Javascript 数组的$_SESSION键没有名为“sessionKey”的键。

回答by Pieter

You can use cookies instead of sessions. See this tutorial: http://tutorialzine.com/2010/03/microtut-getting-and-setting-cookies-with-jquery-php/

您可以使用 cookie 代替会话。请参阅本教程:http: //tutorialzine.com/2010/03/microtut-getting-and-setting-cookies-with-jquery-php/

(Of course you should encrypt it)

(当然你应该加密它)