title: fetching php SESSION data in JSON fromat into html via jQuery and ajax
body: <p>I'm facing a head messing situation while retrieving PHP SESSION details via ajax and jQuery.</p>
  
  <p>Let me explain the whole situation.</p>
  
  <p>My <code>fetch.php</code> [get and send the SESSION details]:</p>
  
  <pre><code> header('Content-Type: application/json');
   session_name('admin');
   session_set_cookie_params(2*7*24*60*60);
   session_start();
   echo json_encode(array(
     'session'=&gt;$_SESSION,
     'session'=&gt;array(
       'user'=&gt;$_SESSION['user'],
       'email'=&gt;$_SESSION['email'],
       'rememberMe'=&gt;$_SESSION['rememberMe']
     ),
   ));
   exit(0);
  </code></pre>
  
  <p>jQuery code to fetch the details :</p>
  
  <pre><code> $.ajax({
     url: "fetch.php",
     type: "get",
     dataType: 'json',
     cache: false,
     success: function(data){
       user = data.session.user;
       email = data.session.email;
       rememberMe = data.session.rememberMe;
     }
   });
  </code></pre>
  
  <p>After fetching, I'm trying to add/remove something to my html [based on the session details]:</p>
  
  <pre><code> if(user != null &amp;&amp; user != undefined &amp;&amp; user != "undefined"){
     //if 'user' is not 'null/undefined', it means that he is logged in, so add something
   }else{
     //remove the added item
   }
  </code></pre>
  
  <p>MAIN PHP :</p>
  
  <pre><code> header('Content-Type: application/json');
   session_name('admin');
   session_set_cookie_params(2*7*24*60*60);
   session_start();
  
   if($_SESSION['user'] &amp;&amp; !isset($_COOKIE['remember']) &amp;&amp; !$_SESSION['rememberMe']){
     $_SESSION = array();
     session_destroy();
   }  
  
   //......
  
   if($_REQUEST['Login']){
     // ....
     $_SESSION['user']=$name;
     $_SESSION['email']=$email;
     if(isset($_POST['rememberMe'])){
       $_SESSION['rememberMe'] = $_POST['rememberMe'];
       setcookie('remember',$_POST['rememberMe']);
     }else{
       setcookie('oneTime','1');
     }
     // ....
   }
  
   //.......
  
   if($_REQUEST['Logout']){
     $_SESSION = array();
     session_destroy();
     $admin['msg'] = "Logout successful";
   }
  </code></pre>
  
  <p>Now, the main problem is, this code behave mysteriously/unpredictable...
  for testing purpose, I add </p>
  
  <pre><code>  alert(user);
  </code></pre>
  
  <p>just after my ajax code.</p>
  
  <p>So, When I load the page, it alerts <code>undefined</code>, its good.
  But when I logged-in successfully {for example, logging-in with "try" user} it alerts <code>null</code> rather than the user name, and add the dynamic element to the html. It means, totally ignoring the <code>if</code> condition.</p>
  
  <p>And when I refresh the page without logging-out, it alerts <code>undefined</code> and didn't remove the dynamically added element.</p>
  
  <p>While I logout the user, it alerts the real user name {"try"} and remove the dynamically added elements [again, ignoring the <code>if</code> condition].</p>
  
  <p>here is my jQ "login" code:</p>
  
  <pre><code> $.ajax({
     url: $frm.attr('action')+'?'+name+'=true',
     type: $frm.attr('method'),
     data: serial,
     success: function(response){
       //.....
     }
   });
  </code></pre>
  
  <p>can somebody tell me that where i'm doing wrong....</p>
  
  <p>And sorry for typo OR grammatical mistakes.</p>
  
  <p>UPDATE:</p>
  
  <p>some code from my <code>main.js</code>:</p>
  
  <pre><code> var handler = {
     init: function(){
       $.ajax({
         url: "fetch.php",
         type: "get",
         dataType: 'json',
         cache: false,
         success: function(data){
           user = data.session.user;
           email = data.session.email;
           rememberMe = data.session.rememberMe;
         }
       });
       alert(user);  // echoing the value of user
       if(user != null &amp;&amp; user != undefined &amp;&amp; user != "undefined"){
         //....... dynamically add the element is user is logged-in
       }else{
         //...... remove the dynamically added element
       }
       handler.submit();
     },
     submit: function(){
      $('html').on('click','.submit',function(e){
         var $frm = $(this).parents('form');
         var serial = $frm.serialize();
         var name = $(this).attr('name');
         $.ajax({
           url: $frm.attr('action')+'?'+name+'=true',
           type: $frm.attr('method'),
           data: serial,
           success: function(response){
               //....... on success : some code
           }
         });
         setTimeout(function(){handler.init();},5000);
         e.preventDefault();
       });
     }
   };
   $(document).ready(handler.init);
  </code></pre>