note 54023 added to function.set-time-limit

From: Date: Tue, 21 Jun 2005 10:08:30 +0000
Subject: note 54023 added to function.set-time-limit
Groups: php.notes 
Request: Send a blank email to php-notes+get-90733@lists.php.net to get a copy of this message
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(); } ?> ---- Manual Page -- http://www.php.net/manual/en/function.set-time-limit.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+54023 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54023&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54023&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54023&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54023&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54023&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54023&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+54023&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#90733) next »