title: How to pass session object with JSON response in php?
body: <p>Before giving detail information on Question, I want to tell that architecture of project, project is design in three separate parts<br>
  <strong>1.</strong> UI Part, this is in <strong>Pure HTM-5,</strong> JavaScript, Ajax, JSON, RestFull API, there is <strong>no any embedded code like PHP, jSP</strong>.<br>
  <strong>2.</strong> Service Part, service is written in PHP, URL of these services access by UI part.<br>
  <strong>3.</strong> DataBase<br>
   <strong>I don't want to add any PHP code in HTML file</strong>   </p>
  
  <p>Now I want to create login form in pure html page, After successfully login it redirect to userProfile, Now user able to update the profile, I want to main session for this type scenario. 
  I am successfully able to login and redirect to userProfile with info Which I fetch from server using JSON, and send this info to userProfile with help of storeUserDataInSession. <strong>Now I want to add Update and Logout facility without send again userName and password</strong>, please help me on this, I am new in session management.  </p>
  
  <p><strong>JavaScript for Login form</strong></p>
  
  <pre><code>&lt;script type="text/javascript"&gt;  
      $(document).ready(function(){
  
                  var userData = {
                   storeUserDataInSession: function(userData) {
                       var userObjectString = JSON.stringify(userData);
                       window.sessionStorage.setItem('userObject',userObjectString)
                   },
                   getUserDataFromSession: function() {
                       var userData = window.sessionStorage.getItem('userObject')
                       return JSON.parse(userData);
                   }
                  }   
  
                 $("#register-form-header").submit(function(){            
  
                  var uName = $('#userName').val();               
                  var upassd=$('#password').val();    
                  $.ajax({                                        
                      url:"http://localhost/login/login.php",                     
                      type:"POST",
                      dataType:"json",
                      data:{type:"login",uName:uName,password:upassd},                    
  
                      ContentType:"application/json",
                      success: function(response){
  
                          userData.storeUserDataInSession(response);
  
                          window.location = 'userProfile.html';
  
                      },
                      error: function(err){                           
                          //alert(JSON.stringify(err));
                          alert("fail");
                          window.location.href = 'error.html';
                      }           
                  });
                  return false; 
  
          });
          //loadJSON(0);
      });
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>JavaScript for UserProfile</strong> </p>
  
  <pre><code>&lt;script&gt;             
    $( document ).ready(function() {
         var userData = {
           storeUserDataInSession: function(userData) {
               var userObjectString = JSON.stringify(userData);
               window.sessionStorage.setItem('userObject',userObjectString)
           },
           getUserDataFromSession: function() {
               var userData = window.sessionStorage.getItem('userObject')
               return JSON.parse(userData);
           }
          } 
  
          var userDataObject=userData.getUserDataFromSession();
          var id =userDataObject[0].userInfo.id;
          var username =userDataObject[0].userInfo.username;
          var name =userDataObject[0].userInfo.name;
          var address =userDataObject[0].userInfo.address;
          var mobile =userDataObject[0].userInfo.mobile;
          var email =userDataObject[0].userInfo.email;            
          document.getElementById('welcomeUser').innerHTML="Welcome Mr"+username;
          document.getElementById('userName').value=username;
          document.getElementById('Name').value=name;
          document.getElementById('email').value=email;
          document.getElementById('mobile').value=mobile;
          document.getElementById('address1').value=address;
  
  
    }); 
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>Login service in php</strong></p>
  
  <pre><code>&lt;?php
      header('Access-Control-Allow-Origin: *');//Should work in Cross Domaim ajax Calling request
      include("config.php");
      //session_start();
      if(isset($_POST['type']))
      {
          if($_POST['type']=="login"){
              $name = $_POST ['uName'];             
              $password=$_POST ['password'];
              $query="SELECT id, username, address, name, mobile, email FROM admin WHERE UserName='nee' AND PASSWORD='12'";           
              $result=mysql_query($query);
              $totalRows=mysql_num_rows($result); 
              if($totalRows&gt;0){
                  $recipes=array();
                  while($recipe=mysql_fetch_array($result, MYSQL_ASSOC)){
                      $recipes[]=array('userInfo'=&gt;$recipe);
                  }
                  echo json_encode($recipes);         
              }   
          }
      }
      else{
          echo "Invalid format";
      }
  ?&gt;
  </code></pre>