weixin_33725270 2015-03-12 09:50 采纳率: 0%
浏览 115

$ _SESSION在AJAX请求中丢失

I've got a Smarty project which works with jQuery as well, where I struggled about an issue some days ago. index.php

  session_start();
  require_once 'controller.php';

 *//nothing else happens here*

I load my controllers and templates then

controller.php

<?php
    isset($_GET[controller]) ? $controller="/".$_GET[controller] : $controller = "/index";
    isset($_GET[department]) ? $department="/".$_GET[department] : $department = "/index";

   require_once "controller".$department.$controller.".php";
   $smarty->display("templates" . $department . $controller . ".tpl");

on page load my jQuery auto loads different sub-templates and I've echoed the $_SESSION content at the beginning of each controller file

Here's my JS

autoload_subtemplate = function(data) {

var target    = data.target;
var id        = data.id;

*//fill target container*
var page2load = data.page.split("_");

$.get("http://"+GLOB_ENV+"MYDOMAIN/"+page2load[0]+"/"+page2load[1]+"?"
  + "import=1"
  + "&data=" + JSON.stringify(data),function(content){

    $("#"+target).html(content);

    *//searching for content to autoload*

    if( $('#'+target).parent().attr("id")=="content" ){

      $("a.auto.loadsubtpl").each(function(){

        var load = $(this).attr("loadtarget");
        var page = $(this).attr("loadpage").split("_");

       //****************************************************
       //the file, loading now, returns an empty $_SESSION
       //NOTE: it's the same action, as the $.get some lines above
       //where the SESSION is NOT lost!!!
       //****************************************************
        $.get("http://"+GLOB_ENV+"MYDOMAIN/"+page[0]+"/"+page[1]
          +"?import=1"
          +"&data=" + JSON.stringify(data),function(details){

          $("#"+load).append(details);
          $(this).addClass("active");
          $("#"+load).show();
        });
      });
    }
  }
);
return false;
};

Now the weird thing: If I put an echo "whatever"; into my root/index.php, right after the session_start(); the $_SESSION is still there.

As this workaround is kinda stupid i'm wondering if there is someone out there, who has a explanation for this.

I simply call print_r($_SESSION) in the regarding php files, i'm loading via AJAX.

  • 写回答

1条回答 默认 最新

  • 撒拉嘿哟木头 2015-03-12 09:59
    关注

    Looks like you haven't called session_start() on your Ajax page

    评论

报告相同问题?

悬赏问题

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