weixin_33725272 2015-11-22 20:42 采纳率: 0%
浏览 53

用$ .ajax更改$ _SESSION

The actual page launch an $.ajax (POST) request and the receiver.php need to modify the $_SESSION. However it do not work, i don't know whether the id of $_SESSION is different or if i need a refresh on the page for register the modification.

I tried to transfer the session id and modifiy it with :

<?php
    if ( count( $_POST ) > 1 && isset( $_POST["s"] ) )
    {
        session_id( $_POST["s"] );
        session_start();
    }
?>

but this code is ineffective...

My $.ajax request is simple :

var s = <?php echo "'" . session_id() . "'"; ?>;

$.ajax({
    url: 'jquery/receiver.php',
    type: 'POST',
    dataType: 'html',
    data: {
        s: s, // session id, useless?
        // data...
    },
}).done(function ( data ) {
    // operations ...
}

My receiver.php too ;

<?php
    if ( count( $_POST ) > 1 && isset( $_POST["s"] ) )
    {
        session_id( $_POST["s"] );
        session_start();
    }

    $_SESSION['longarray']['key'] = 'value';
?>

Finally, it is possible to modify $_SESSION with $.ajax (POST) ? Or maybe i have only a problem because my $_SESSION is a array ?

  • 写回答

2条回答 默认 最新

  • weixin_33688840 2015-11-22 20:50
    关注

    Ok I tried the following testp1.php

        <?PHP
            session_start();
        ?>
        <script type="text/javascript" src="../../common/jquery/jquery-1.10.2.min.js"></script>
        <script>
        $(document).ready(function () {
            var s = '<?php echo session_id(); ?>';
    
            $.ajax({
                url: 'receiver.php',
                type: 'POST',
                data: {
                    s: s, // session id, useless?
                    // data...
                },
                success: function(data){ console.log(data); }
            })
        })
        </script>
        <body>
        <?PHP
            echo $_SESSION['longarray']['key'];
        ?>
        </body>
    

    ajax call to receiver.php

    <?php
        if ( count( $_POST ) >= 1 && isset( $_POST["s"] ) )
        {
            session_id( $_POST["s"] );
            session_start();
            $_SESSION['longarray']['key'] = 'mix';
        }
        else
            echo "ERROR";;
    ?>
    

    On testp1.php the body shows mix

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮