斗士狗 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 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试