Q:

Session does not get updated value in php

I am calling firstpage.php it changing $_SESSION['Progress'] session value. But when I call progress.php for get $_SESSION['Progress'] session value it keep gives "1" value. progress.php doesn't gives updated value. I checked manually firstpage.php working fine. but I don't know why progress.php doesn't give updated value. 
var progressInterval;
  $.ajax({
          type: "POST",
          url: "firstpage.php",
          cache: false,
          data: {
            first: first
          },
          success:function(result)
          {
            if(progressInterval) {
                clearInterval(progressInterval); //if I remove this session still show "1" output
            }
            alert(result); // print session value. ex:- value- 1,2,3,4,5,6,7,8,Final=8
          }
  });

  progressInterval = setInterval(function(){
      $.ajax({
          url:'progress.php',
          type: 'get',
          data: {"name":"Progress"},
          dataType:'json',
          success: function(data) {
              $('.output').text((parseInt(data.progress))+"%"); // KEEP SHOWING 1% (why value does not change?????)
          },
          error:function(err){
              console.log(err);
          }
      });

  }, 1000);

firstpage.php
session_start();
$_SESSION['Progress'] = 0;
  $data=0;
  $progress=$_SESSION['Progress'];
  foreach($album_data as $row) //assume foreach works fine.
    {
        $progress++;
        $_SESSION['Progress'] = $progress;
        $data.=$progress.",";
        session_write_close();
    }
  }
  echo $data." final=".$_SESSION['Progress']; //output-1,2,3,4,5,6,7,8 final=8

progress.php code
session_start();

getProgress($_GET['name']);

function getProgress($filename) {
    if (isset($_SESSION[$filename])) {
        echo json_encode(array("progress" => $_SESSION[$filename]));
    } else {
        echo json_encode(array("progress" => 0));
    }
}

A:

session_write_close(); ends the current session and store session data. 
At each iteration of the loop you will need to restart the session using session_start();

