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

时间:2021-10-05 23:37:14

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.php which 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.

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

Does session does not work from AJAX requests?

会话在AJAX请求中不起作用吗?

7 个解决方案

#1


27  

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请求。

#2


5  

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)

#3


3  

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”之前有一个小的,很小的,很难看到的空间。这最终发回信息并禁止会话开始,因为标题信息已经发送。可能不是其他任何人的情况,但它绊倒了我并带我到这个页面寻找答案。

#4


0  

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之前可以正常工作。

#5


0  

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

#6


0  

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 file的ajax中使用会话变量。我试图通过session_start()启动会话但是“无法修改标题信息”发生。我删除了BOM字符和代码工作很好。

#7


0  

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

在jQuery或JavaScript中,您可以获得如下的会话值:

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

alert(StepIndexval);

#1


27  

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请求。

#2


5  

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)

#3


3  

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”之前有一个小的,很小的,很难看到的空间。这最终发回信息并禁止会话开始,因为标题信息已经发送。可能不是其他任何人的情况,但它绊倒了我并带我到这个页面寻找答案。

#4


0  

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之前可以正常工作。

#5


0  

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

#6


0  

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 file的ajax中使用会话变量。我试图通过session_start()启动会话但是“无法修改标题信息”发生。我删除了BOM字符和代码工作很好。

#7


0  

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

在jQuery或JavaScript中,您可以获得如下的会话值:

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

alert(StepIndexval);