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

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)