从我的AJAX所谓的PHP脚本为什么我不能访问会话变量? [英] Why can't I access session variables from my AJAX-called PHP script?

查看:102
本文介绍了从我的AJAX所谓的PHP脚本为什么我不能访问会话变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP脚本与会话变量,设置像这样:

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

$_SESSION['VAR1'] = "test"

现在,我通过一个jQuery发起的POST请求使用AJ​​AX,所以我有一个脚本名为 ajax.php 里面有所有需要的功能。

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.

当我尝试使用我的会话变量(回声$ _SESSION ['VAR1'] )的ajax.php,会产生什么。

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

难道会不从AJAX请求工作?

Does session does not work from AJAX requests?

推荐答案

您需要做的每一个访问会话访问前页:

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

session_start();

这意味着无论是设置会话变量和AJAX页面试图检索到页面上。两者都需要调用 在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请求调用在同一域中的脚本(从而获得访问会话cookie),没有理由为什么它无法获得访问会话变量。毕竟一个Ajax请求仅仅是另一个HTTP请求。​​

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所谓的PHP脚本为什么我不能访问会话变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆