Sven S. S. Svensson
Sven S. S. Svensson

Reputation: 11

$_SESSION lost on AJAX request

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.

Upvotes: 1

Views: 1307

Answers (1)

imnotanelephant
imnotanelephant

Reputation: 862

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

Upvotes: 1

Related Questions