Page 1 of 1
Track PHP code progress
Posted: Sat Jun 05, 2010 11:31 am
by J0kerz
Hey there,
I have a php script that can take 10-60 min to complete. I would like to be able to see the progress of this script while its running. I tried adding echo/print but these echo only appear on screen when the script is complete so I cant track the progress. I also tried flushing, but it doesnt work...
What could I do to track the progress of my PHP script while its running?
Thanks!

Re: Track PHP code progress
Posted: Sat Jun 05, 2010 11:38 am
by Jonah Bron
Perhaps something like this?
Main page uses AJAX to initiate PHP script;
PHP script periodically writes progress to text file;
Main page periodically reads the text file with AJAX and displays it;
Re: Track PHP code progress
Posted: Sat Jun 05, 2010 11:52 am
by J0kerz
IS there a way to achieve what you just said without writing to a text file? Let say store data in memory or something like that.
Re: Track PHP code progress
Posted: Sat Jun 05, 2010 12:02 pm
by Jonah Bron
Don't know. You might try to see if AJAX will progressively display the response. But, instead of showing it when readyState is 4, use setInterval() to display the responseText every few seconds.
Re: Track PHP code progress
Posted: Sat Jun 05, 2010 2:03 pm
by J0kerz
Even if I use Javascript, everything appear on screen only when the script is over. ANy idea how can I get this working?
What are you guys doing when you are dealing with a php script running for a long time to track the progress?
Code: Select all
<?php
session_start();
$_SESSION['message'] = 'Start';
?>
<html>
<head>
<script type="text/javascript">
function update() {
message = "<?php echo $_SESSION['message']; ?>";
document.getElementById('myDiv').innerHTML = message;
}
setInterval('update()',1000);
</script>
</head>
<body>
<div id="myDiv" style="font-weight: bold;">Heyyy</div>
<?php
for($i=0; $i < 10; $i++){
$_SESSION['message'] = $i;
sleep(1);
}
?>
</body>
</html>
Re: Track PHP code progress
Posted: Sat Jun 05, 2010 2:07 pm
by Eran
use flush() to see output while the script is still running (read the full description of the function for possible pitfalls)
http://php.net/manual/en/function.flush.php
Re: Track PHP code progress
Posted: Sat Jun 05, 2010 2:19 pm
by J0kerz
I tried flush with no succes...
Example of code that isnt working:
Code: Select all
<?php
for($i=0; $i < 10; $i++){
echo $i . '<br />';
ob_flush();
flush();
sleep(1);
}
?>
Re: Track PHP code progress
Posted: Sat Jun 05, 2010 4:00 pm
by Eran
Did you read the function description? there are several outside factors that could influence whether output is flushed or not.
Re: Track PHP code progress
Posted: Sun Jun 06, 2010 4:06 pm
by J0kerz
Yes I did but I cant seem to find a solution either...
Guess Ill need to use ajax.