简体   繁体   中英

Javascript getting progress of PHP-script

I need to provide interaction between js on html-page and php-script.
- Use AJAX.
Ok. But the problem is that php-script executing for a long time, and i need to know the state of this script processing (eg 60% complete)
What should i do? Create 2 php-scripts (client&server) and do ajax-request to client.php which will do requests to server.php via sockets or smth? Are there more elegant solutions?

What if you had the script doing the processing write its status to a file once in awhile. Make a second script that will read the file and return the status of the original one.

  1. Ajax call php script and return information that script is runing.
  2. Main script create lock.file.
  3. Script called from cron is checking if lock.file exists and run the correct script.
  4. The correct script saves the current progress into progress.txt.
  5. Ajax is reading progress.txt and when progress is 100% then return information that script processing is finished.

edited: Thanks to Justin for poiting the timeout problem ;)

You should never have a long-running process being executed entirely within an HTTP session.

A simple and common approach to this problem is message queuing. Basically, you have your UI queue up the request into a database table and then have external daemon(s) process the queue.

To provide feedback, have the daemon periodically update the table with the status for the row it's currently working on. Then, your javascript code can make AJAX requests to a script that retrieves the status for that work item from the database and displays it to the user.

See: Dealing with long server-side operations using ajax?

If you want to be really fancy, write output from the php script to stdout, and capture it via a pipe. This would require running the php script using exec() or proc_open() (http://php.net/manual/en/function.proc-open.php) and pipe the output to a file (or if you want to be extra-extra fancy, use node.JS to listen for that data)

There are quite a few ways to accomplish this:

  • Node.JS
  • An Ajax query every x seconds
  • A META/Javascript page reload
  • An iframe that is routinely reloading with the status in it.

Good luck!

You could use PHP's output buffering (see ob_flush ) to flush the contents at certain points in your script and tailor your JavaScript so that it uses the flushed contents. I believe readyState in your AJAX call won't be set to 4 on flushes so that's where you'll have to handle that yourself (see this article ). I think its a much nicer way than writing to a file and checking the contents of that.

on your process.php :

// 1st task
$_SESSION['progress'] = 0;

// your code for the first task here ...

// 2nd task
$_SESSION['progress'] = 10;

// you code for 2nd task ...

// 3rd task
$_SESSION['progress'] = 17;

// continue ...

// everything finished?
$_SESSION['progress'] = 100;

on your progress.php :

// simply output
echo $_SESSION['progress'];

now from your client-side, just make a request to your progress.php , receive the number and give it to your progress bar ...

didn't check that by myself, but hope that it works! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM