note 54023 deleted from function.set-time-limit by googleguy
| From: | googleguy@php.net | Date: | Fri, 11 Jan 2013 17:25:22 +0000 |
| Subject: | note 54023 deleted from function.set-time-limit by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-192935@lists.php.net to get a copy of this message | ||
Note Submitter: Sjoerd van der Hoorn
----
When using the set_time_limit() function, the browser will stop after about 30 seconds if it does
not get new data. To prevent this, you can send every 10 seconds a little snippet of data (like a
single character) to the browser. The code below is tested with both Internet Explorer and Firefox,
so it will stay online all the time.
You should also create a file called chatdata.txt which contains the last thing said on a chatbox.
Please note that you can also replace this function with a MySQL or other database function...
<?php
set_time_limit(900);
// Start output buffering
ob_start();
$message = "First test message";
$oldmessage = "bla";
// Keep on repeating this to prevent PHP from stopping the script
while (true)
{
$timeoutcounter = 0;
while ($message == $oldmessage)
{
// If 10 seconds elapsed, send a dot (or any other character)
if ($timeoutcounter == 10)
{
echo ".";
flush();
ob_flush();
$timeoutcounter = 0;
}
// Timeout executing
sleep(1);
// Check for a new message
$message = file_get_contents("chatdata.txt");
$timeoutcounter++;
}
// Keep the old message in mind
$oldmessage = $message;
// And send the message to the user
echo "<script>window.alert(\"" . $message .
"\");</script>";
// Now, clear the output buffer
flush();
ob_flush();
}
?>