为什么我不能从我的 AJAX 调用的 PHP 脚本访问会话变量?

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

Why can't I access session variables from my AJAX-called PHP script?

phpjqueryajax

提问by cletus

I have one PHP script with a session variable, set like so:

我有一个带有会话变量的 PHP 脚本,设置如下:

$_SESSION['VAR1'] = "test"

Now, I am using AJAX via a jQuery-initiated POST request, and so I have a script named ajax.phpwhich has all the required functions.

现在,我通过 jQuery 启动的 POST 请求使用 AJAX,因此我有一个名为的脚本ajax.php,其中包含所有必需的功能。

And when I try access my session variable (echo $_SESSION['VAR1']) in ajax.php, it produces nothing.

当我尝试echo $_SESSION['VAR1']在 ajax.php 中访问我的会话变量 ( ) 时,它什么也没产生。

Does session does not work from AJAX requests?

会话对 AJAX 请求不起作用吗?

回答by cletus

You need do this on every page that accesses the session before you access it:

在访问会话之前,您需要在访问会话的每个页面上执行此操作:

session_start();

That means on both the page that sets the session variable and the AJAX page that tries to retrieve it. Both need to call session_start().

这意味着在设置会话变量的页面和尝试检索它的 AJAX 页面上。两者都需要调用session_start()

As long as the AJAX request calls a script in the same domain (and thus gets access to the session cookie) there is no reason why it couldn't get access to the session variables. An AJAX request after all is just another HTTP request.

只要 AJAX 请求调用同一域中的脚本(从而可以访问会话 cookie),就没有理由无法访问会话变量。AJAX 请求毕竟只是另一个 HTTP 请求。

回答by Salman A

Make sure that the domain names for both pages (i.e. the AJAX container and the AJAX script are same). Here is an example:

确保两个页面的域名(即 AJAX 容器和 AJAX 脚本相同)。下面是一个例子:

http://mydomain.com/login.php           (set session variables here)
http://mydomain.com/ajax-container.php  (session variables are visible here)
http://mydomain.com/ajax-script.php     (session variables are visible here)
http://www.mydomain.com/ajax-script.php (session variables are NOT visible here)

Another one:

另一个:

http://www.mydomain.com/login.php          (set session variables here)
http://www.mydomain.com/ajax-container.php (session variables are visible here)
http://www.mydomain.com/ajax-script.php    (session variables are visible here)
http://mydomain.com/ajax-script.php        (session variables are NOT visible here)

回答by user875479

I also caught myself on having one little, tiny, hard to see, space just before "< ? php " This ended up sending information back and disallowing the session to start because header information was already sent. May not be the case for anyone else, but it tripped me up and brought me to this page in search of an answer.

我还发现自己在 "< ? php " 之前有一个很小的、很难看到的空间,这最终将信息发回并禁止会话开始,因为头信息已经发送。其他人可能不是这种情况,但它让我绊倒并带我到这个页面寻找答案。

回答by codeymcgoo

An addendum to what Salman A wrote:

Salman A 所写内容的附录:

If you set a session variable in an https:// file and try to access it with a http:// file you will not be able to...

如果您在 https:// 文件中设置会话变量并尝试使用 http:// 文件访问它,您将无法...

https://www.example.com/index.php - call session_start() and set session variable
http://ww.example.com/index_tmp.php - unable to access session variable

and vice versa...

反之亦然……

http://www.example.com/index.php - call session_start() and set session variable
https://ww.example.com/index_tmp.php - unable to access session variable

Rather:

相当:

https://www.example.com/index.php - call session_start() and set session variable
https://ww.example.com/index_tmp.php - Able to access session variable

And:

和:

http://www.example.com/index.php - call session_start() and set session variable
http://ww.example.com/index_tmp.php - Able to access session variable

回答by Yoones Mehdian

My own error was BOM character in my ajax file.I was need to use session variable in a ajax called php file.I tried to start session by session_start() but "cannot modify header information" occurs.I removed BOM character and code works very well.

我自己的错误是我的 ajax 文件中的 BOM 字符。我需要在名为 php 文件的 ajax 中使用会话变量。我尝试通过 session_start() 启动会话,但发生“无法修改标题信息”。我删除了 BOM 字符并且代码有效很好。

回答by user3511084

In jQuery or JavaScript, you can get the session value like this:

在 jQuery 或 JavaScript 中,您可以像这样获取会话值:

var StepIndexval = '<%= Session["StepIndex"].ToString() %>';

alert(StepIndexval);

回答by Kneel-Before-ZOD

Make sure no content has been echoed (not even a whitespace) before calling session_start().
To be safe, put the code as the first code of whatever template you used for the page. The function will not work if content has been sent to the browser.
To test and see where the problem is, call the page as a stand-alone, instead of through AJAX and ensure that it works before AJAXing it.

确保在调用 session_start() 之前没有回显任何内容(甚至没有空格)。
为安全起见,请将代码作为您用于页面的任何模板的第一个代码。如果内容已发送到浏览器,则该功能将不起作用。
要测试并查看问题出在哪里,请将页面作为独立调用,而不是通过 AJAX 调用,并在 AJAX 调用之前确保它可以正常工作。