I’m using pctnl_fork to break a large task into multiple processes and it’s working great except for one annoying thing: for some reason the output has “Content-type: text/html” embedded in it and nothing I do seems able to get rid of it.
Here’s a working example of the issue:
<?php
ob_start();
$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
} else if ($pid) {
// we are the parent
pcntl_wait($status); //Protect against Zombie children
} else {
$fh=fopen('/var/www/html/test.txt','w');fwrite($fh, time());fclose($fh);
exit(0);
}
header_remove();
ob_end_clean();
echo " done";
?>
If I omit the header_remove() then it outputs
X-Powered-By: PHP/5.3.3 Content-type: text/html done
With header_remote it gets rid of the X-Powered-By header but the Content-type header remains:
Content-type: text/html done
I’ve pulled my hair out trying to fix this to no avail. I thought for sure ob_end_clean() would fix it but it doesn’t. If anyone has any suggestions I’d be eternally grateful.
Don’t use
pcntl_fork()from within web server environments (such as mod_php). As you’ve observed, it does not function correctly when running within the web server, and as such it is not available outside the CLI SAPI in newer versions of PHP.