2

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));
    }
}
1
  • show us how did you set the session in firstpage.php Commented Sep 6, 2014 at 4:46

3 Answers 3

2

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();

Sign up to request clarification or add additional context in comments.

17 Comments

Sorry I already added session_start() in the starting line but I forgot to mansion in code. in firstpage.php session works fine. it also gives the output. but I didn't get from progress.php page
but what are you doing with $filename I didnt get that part?
nothing but I am just trying to get the value of $_SESSION['Progress']. And i thing It should be give.
If I reomove clearInterval(progressInterval); I still give me output 1
@user3730347 remove that line session_write_close();
|
2

I changed in firstpage.php. and it works now.

session_start();
$_SESSION['Progress'] = 0;
 $data=0;
  $progress=$_SESSION['Progress'];
  foreach($album_data as $row) //assume foreach works fine.
   {
    session_start(); //we need to start session because we closed the session using session_write_close(); 
    $progress++;
    $_SESSION['Progress'] = $progress;
    $data.=$progress.",";
    session_write_close(); // need to close because we getting the value uses at progress.php. 
   }
 }
 session_start();
 echo $data." final=".$_SESSION['Progress']; //output-1,2,3,4,5,6,7,8 final=8

1 Comment

No we need to start every time
0

You migh try this. you have index.php which fetch value of $_SESSION['Progress'] here is what index.php contain

index.php

<html>
<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script>
        var progressInterval;

        progressInterval = setInterval( function(){
        $.ajax({
              url: "progress.php",
              data: { name: 1 }
            }).done(function(msg) {
               $("#test1").text(msg);
            });
        }, 2000);
    </script>
</head>
<body>
    <p id='test1' class='output'></p>
</body>
</html>

it will fetch all $_SESSION['Progress'] every 2 seconds.

And here what progress.php contain

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

and then for 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
  */

firstpage.php work just for change $_SESSION['Progress'] value.

Try to open 2 windows of browser. one for index.php and one for firstpage.php.

Keep index.php open, while you change the value of $_SESSION['Progress'] in firstpage.php to 2(for example) and reload page firstpage.php. then, you will found out the value of $_SESSION['Progress']which return by ajax request in index.php change to {"progress":2}

So your problem I think on performing ajax request.

2 Comments

I think the problem is session lockingin firstpage.php page. I try to inspect element. and the status of progress.php is "pending" when the firstpage.php process done then process.php return the output
I Guest so. I think you should extend your get session request interval and make sure set session request called first using window.onload or document.onload to call get session ajax request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.