Track PHP code progress

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
J0kerz
Forum Commoner
Posts: 37
Joined: Fri May 29, 2009 2:51 pm

Track PHP code progress

Post 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!
:D
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Track PHP code progress

Post 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;
User avatar
J0kerz
Forum Commoner
Posts: 37
Joined: Fri May 29, 2009 2:51 pm

Re: Track PHP code progress

Post 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.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Track PHP code progress

Post 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.
User avatar
J0kerz
Forum Commoner
Posts: 37
Joined: Fri May 29, 2009 2:51 pm

Re: Track PHP code progress

Post 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>
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Track PHP code progress

Post 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
User avatar
J0kerz
Forum Commoner
Posts: 37
Joined: Fri May 29, 2009 2:51 pm

Re: Track PHP code progress

Post 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);
}
?>
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Track PHP code progress

Post by Eran »

Did you read the function description? there are several outside factors that could influence whether output is flushed or not.
User avatar
J0kerz
Forum Commoner
Posts: 37
Joined: Fri May 29, 2009 2:51 pm

Re: Track PHP code progress

Post by J0kerz »

Yes I did but I cant seem to find a solution either...

Guess Ill need to use ajax.
Post Reply