I've written these before in Perl, in different contexts and for another purpose. I'll lay out the rough logic, should be easy to duplicate in PHP.
You would use fork (or if installed, pcntl_fork) to fork a child process. The
process id of the child is what is returned by fork. This is where you do your time intensive process. Perl-ese, PHP is similar:
if ($pid = fork()) {
// child process
}
else {
//parent
}
So the parent now has a process ID as a "Handle" on the running process. The parent outputs an iframe that has a src to a second script; you pass the $pid to this second script (or, call a function in the same script, doesn't matter.)
There are easier methods, but I just do a system exec on px aux. This returns a set of rows in an array of all current processes. We're only looking for THIS process. So,
$status = system_exec('ps aux');
$lines = preg_split('/\n+\r*/',$status);
foreach ($lines as $line) {
$line = preg_replace('/\s+/',' ',$line); // tabs, multiple spaces for single spaces
list($user,$process_id,$cpu,$mem,$vsz,$rss,$tty,$stat,$start,$time,$cmd) = preg_split('/\s+/',$line);
$cmd is an ARRAY
if ($process_id eq $current_p) {
$this_status = preg_match("/$this_script/",$cmd[count($cmd-1)])?'Processing ok':$cmd[count($cmd-1)];
break;
}
}
Some stuff may be lost in the PHP translation, but that's the gist. The code of the running process will exist in the last member of the $cmd array. As long as the process exists, it will have a result code to return. When it's gone, it has no value to return.