斗士狗 2020-03-30 16:17 采纳率: 0%
浏览 88

会话ID丢失(ajax + PHP)

I'm having an issue with AJAX + PHP session ID where a new ID is getting generated by the script getting grabbed via GET. I've created 2 test scripts that expose the problem:

script 1 (commented out the AJAX, but both XMLHttpRequest and $.get() yield the same result):

<?php
    session_start();
    ob_start();
    echo('test: '.session_id());

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="test"></div>
    <script type='text/javascript' src='../node_modules/jquery/dist/jquery.js?<?echo($time)?>'></script>
    <script type="text/javascript">
        $(document).ready(function() {
            var xhr = new XMLHttpRequest()
            xhr.onreadystatechange = function () {
                if(xhr.readyState === 4 && xhr.status === 200) {
                    document.getElementById('test').innerHTML=this.responseText;
                    resolve('page loaded');
                }
            }
            xhr.open("GET",'http://10.1.1.101/testLinks/test2.php');
            xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xhr.send();
            // $.get('test2.php').done(function(val) {
            //     document.getElementById('test').innerHTML=val;
            // })
        });
    </script>
</body>
</html>

Script 2:

<?php
    session_start();
    ob_start();
    echo('test2: '.session_id());
?>

Output:

test: 4occu9mq6o3c16au254grf0s56
test2: 6bk1fdbs3v2bcl9iko31s5nqp4

Looking at other threads, I've verified the php setting for cookies enabled, as well as the lifetime:

php session info

I've verified that when I include 'test2.php'; a new session ID is not generated. Only when using AJAX.

I'm not sure what else to look at here. Any suggestions appreciated.

EDIT: for clarity, I'm using JQuery 3.4.0, however, after switching to XMLHttpRequest, I don't believe it's a JQuery error, and has more to do with some configuration of PHP, but I'm not sure what.

  • 写回答

1条回答 默认 最新

  • weixin_33736649 2020-04-02 19:08
    关注

    We ended up figuring out the issue. The server we had this deployed on was running the application through http (port 80). We generated an SSL certificate and switched over to HTTPS (port 443), and the cookies were able to hold the PHPSESSID without regenerating. This may be related to some security settings in the organization, however, but hopefully this helps someone having the same issue.

    评论

报告相同问题?

悬赏问题

  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)