在 AJAX 请求上清空 $_SESSION 超全局变量,但保留会话 ID [英] Empty $_SESSION superglobal on AJAX request, but session ID is preserved

查看:59
本文介绍了在 AJAX 请求上清空 $_SESSION 超全局变量,但保留会话 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 $_SESSION 超全局 AJAX 请求时遇到问题.

I'm having problems with $_SESSION superglobal on AJAX request.

session_start() 函数在任何会话编码之前被调用.调用代码和 AJAX 响应代码中的会话 ID 也相同(通过在两个脚本中回显 session_id() 进行测试).AJAX PHP 文件在同一个域中.一切都应该按照标准的定义工作,但是当我在被调用的 AJAX 脚本文件中执行 print_r($_SESSION) 时,我得到了 Arrray( ) 输出.

session_start() function is called before any session coding. Session ID is also the same in the calling code and the AJAX response code (tested by echoing session_id() in both scripts). AJAX PHP file is on the same domain. Everything should work as defined by standards, but when I do print_r($_SESSION) in the called AJAX script file I get Arrray( ) output.

我撞到了砖墙......我不知道为什么这不起作用......

I've hit the brick wall... I don't know why is this not working...

在 Chrome 和 Firefox 中均已检查.

Checked both in Chrome and Firefox.

有什么想法吗?

更新:

问题在于 $.ajax(...) 请求!当我执行 AJAX 请求时,它知道正确的会话 ID,并且 session_start() 函数返回 TRUE(成功继续会话)但随后它会重置我的 $_SESSSION超全局!它清空它......我不知道为什么......

The problem is with $.ajax(...) request! When I do AJAX request it knows right session ID, and the session_start() function returns TRUE (successfully continued session) but then it resets my $_SESSSION superglobal! It empties it out... I don't know why yet...

代码:

index.php:

<?php

session_start();

$_SESSION['Test']='O.K.';

echo("SESSION_ID: " . session_id());
echo("SESSION_SIZE:" . sizeof($_SESSION));

?>

... Standard HTML stuff and jQuery include ...

<script>
    $.ajax(
    {
        type: "POST",
        url: "AJAXTest.php",
        data: null,
        success: function(sData) { alert(sData); }
    });

</script>

AJAXTest.php:

AJAXTest.php:

<?php

session_start();

echo("SESSION_ID: " . session_id());
echo("SESSION_SIZE:" . sizeof($_SESSION));

?>

index.php 输出:

index.php output:

SESSION_ID: xxxxxxxxxxxxxxxxxxxxxxx
SESSION_SIZE: 1

警报输出:

SESSION_ID: xxxxxxxxxxxxxxxxxxxxxxx (right session id)
SESSION_SIZE: 0

在 AJAX 调用之后 $_SESSION 是空的.在具有相同会话的所有其他脚本中......我感到困惑......

And after the AJAX call $_SESSION is empty. Across all other scripts with the same session... I'm baffled...

推荐答案

问题出在我的自定义 php.ini 文件中...它显然搞砸了一些会话重要设置(即使它们没有定义 -> 更改).

The problem was in my custom php.ini file... It obviously screwed up some session important settings (even they were not defined -> changed).

结果是每次调用 session_start() 都会重置 $_SESSION 超全局变量并将其清空,但保留相同的会话 ID,这让我感到困惑并朝错误的方向投掷.直到我把所有东西都剥离到骨头为止,很明显错误不在我的代码中.

Result was that every call to session_start() would reset $_SESSION superglobal and empty it, but leave the same session ID what confused me and threw in the wrong direction. Until I've stripped down to the bone everything it was clear that error was not in my code.

感谢所有感兴趣的人.

这篇关于在 AJAX 请求上清空 $_SESSION 超全局变量,但保留会话 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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