Forum Moderators: coopster
Tried this
$html="....first message goes here..";
echo $html;
ob_flush();
flush();
...
all the processing here
...
echo "<script type='text/javascript' language='javascript'>window.location.href='http://www.example.com';</script>";
I didn't use header because output was already sent and i think it wouldn't work.
Now this solution works fine in most of the cases but when you are talking about like 5000 page loads then 5 percent of the time the redirect is not taking place. I have tried everything to figure out but cant figure out why not.
Just to clearify, the control of execution does get to that line 100% of the time, because just a line above that redirect there is a mysql query which is getting executed 100% of the time.
I have also added
echo '<head><meta http-equiv="refresh" content="1;url=' . $url . '"/></head>';
right there but still there are some failures in high traffic.
Can anybody give me any insight as to why that might be happening? are there any alternatives that i can use to ensure 100% redirection, because i know script control gets there.
Another clue to clearify the situation is that the longer the page takes to process the more chances of a failure, thats what the data suggests.
Any alternatives ?
Thanks
How about testing JavaScript in their browser first and then using AJAX to manage the connection and updates. If it works at the beginning of script execution then you know for certain a refresh/redirect will work upon successful completion because you have control at that point.
We have to send some output to user to tell them to wait and then we keep processing and in the end simply send a url to redirect to.
The answer really lies in a forked process. (PHP-ese: pcntl_fork() [us.php.net], or, if the extension is not installed, do a system call to fork().) When you make a request from a browser, it opens a system process. Depending on the server configuration, the process may or may not persist until it's done. Probably what is happening is the process is dying before it completes. Sometimes it survives, sometimes it doesn't.
With fork, you spawn a child process in which you do all your time-intensive work, and the parent process can now respond immediately to the browser:
$pid = pcntl_fork();
if ($pid) {
// return response to the browser
}
else {
// do your time intensive process here
}
You can do a progress meter type thing by passing the $pid, which is the process ID of the child, to a second script via a refreshed iframe. That is, in the parent "response," you have an iframe with a meta refresh of 5 seconds or so, querying a script to look up the process with this id. If it exists, still in progress, if not, it's done.
$pid = pcntl_fork();
if ($pid) {
$html="
<html><head><title>Working . . .</title></head>
<body><h1>Working . . .</h1>
<p>Progress of your job:</p>
<iframe src=\"job-progress.php?p=$pid\"></iframe>
</body>
</html>
";
echo $html;
}
else {
// do your time intensive process here
}
Contents of job-progress.php should contain a refresh header whether using PHP header or simple meta refresh, like
if (isset($_GET['p']) and ($_GET['p'] > 0)) {
$pid = $_GET['p'];
// look up the process $pid, if it exists, display "working" or something
$html="
<!-- full html document here, just showing the refresh -->
<meta http-equiv=REFRESH CONTENT=\"3; url=job-progress.php?p=$pid\">
";
echo $html;
}
else { die("OOPS no pid to look up . . "); }
Forking has huge applications - large file uploads, intensive time consuming processes, and here's the best part: you don't have to rely on user's patience, and it gives them an immediate response. If they close the window, there's no chance of the child process dying (pending the server rebooting, or something) which there IS in a single process approach.
Some other points to consider: I've had similar issues in the past and sometimes it boils down to internal PHP/database/web server termination settings (ie: it kills the process after #*$! seconds, no matter what).
These are not encountered often (because most web activities are not process-time-intensive). But occasionally they come up. Look into these if you still encounter issues. Another possible issue (unlikely)... memory limit exceeding. If PHP hits its memory limit (which is pretty low by default for any type of processing, like 16mb)... it will cease execution (without even giving an error; I had to check some server logs once to find out what was happening to me one time).