0

I'm trying to get some data from Ajax request when already PHP running a long script ;

but when the Ajax has been answered that script in PHP is completed, and I need to get Ajax response during running other scripts .

JS

setInterval(function(){
    $.post( 'progress.php','',
    function(data){
        $('.-info-').html(data);
    }); 
},100);

progress.php

session_start();
echo $_SESSION['progress'].'%';

running.php

session_start();
$i=0;
while($i<99){
    $i++;
    $_SESSION['progress'] = $i;
    //sleep(1);
    //flush();
}

Is there any way to response when "while" is running?

2
  • wow, wow, see update for my answer Commented Oct 20, 2014 at 15:48
  • Writes about $_SESSION to disk don't happen till the end of the process. The Session file is anyways locked to the serving process, which means the progress.php process will hang till running.php finishes. You'll have to write the progress to your own file and read from there in progress.php. Commented Oct 20, 2014 at 15:49

4 Answers 4

1

Edit: Marc B is right. The session will be locked and not updated as required. So you need to store the progress in an system you can access asynchronously from multiple connections - maybe the database?

running.php:

session_start();
$i=0;
while($i<99){
    $i++;
    // Store progress in Database
}

progress.php:

 session_start();
 //read progress from Database
Sign up to request clarification or add additional context in comments.

2 Comments

I tested all answers , so I got that storing into Database is easy and better .tanQ
I have using progress for copying ,by saving progress in Database and read it by Ajax ... that works fine for small files. but when I chose a file (about more than 100MB) to copy , after a few seconds the all Ajaxs not working .can you help please ? sorry for my bad english
1

PHP doesn't dump out a copy of the saved session data every time you update $_SESSION. The session file is actually LOCKED by PHP when any particular process is using it.

You'd need to do:

while($i<99){
    session_start();  // start session
    $i++;
    $_SESSION['progress'] = $i;

    session_write_close();  // save session data, release session file lock

    sleep(...);
}

In other words, your code would never work, as your running script would keep the session open/locked, preventing progress from ever accessing it.

1 Comment

this working ,but still the output ($_SESSION['progress']) is a fixed value?
1

use flush() to send output to browser and onreadystatechange to get response at client side

$i=0;
while($i<99){
    $i++;
    echo $i; 
    flush();
    sleep(1);
}

an onreadystatechange will be call on every portion of wata from your script ( http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp )

you should check state 3 - State 3 LOADING Downloading; responseText holds partial data. so you easily can use it - you can check it here developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

9 Comments

did you notice which script is he calling from jquery and which one is locking the session file?
onreadystatechange is fired when the state of the connection changes - not when the content of the page changes.
if he'll be print out current status - there is no need to use session - so he can just close session and work with data independently
and state changed on every portion of data wile page loading
well, you are somewhat right. but this will fail for many browsers... IE does not support it at all: Remarks You cannot call the responseText property to obtain partial results (readyState = 3). Doing so will return an error, because the response is not fully received. You must wait until all data has been received. msdn.microsoft.com/en-us/library/ie/ms534361(v=vs.85).aspx
|
0

You can use a global var but the better in mind is make a static or singleton class

class counter {
  private $_progress=0;

  public static function doProgress() {
    $i=0;
    while($i<99){
    $i++;
    $this->_progress = $i;
    }
  }

  public static function getProgress() {
    return $this->_progress;
  }

so in progress.php do

 echo Counter::getProgress()

and in you javascript you must use setinterval() function in my mermory to call at intervaled time the process.php script

2 Comments

in progress.php we have not class Counter ,and if we define it again , it will be a new class . doesn't?
yep. It will be new address space, new process. So static variables won't be shared

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.