=1) { // We don't respond with error for this failure, since it's not // a user problem. dba_delete("msg_$tail", $db); dba_delete("id_$tail", $db); } resp("OK"); } // Build a response. Collect the messages starting with POLL through the // latest, but not more than 5 messages (dropping earlier ones). $messages = array(); $ids = array(); $start = max(1, $head-10, $_REQUEST['POLL']); for($i = $start; $i < $head; ++$i) { $d = dba_fetch("msg_$i", $db); if($d === FALSE) resp("DB read failed", 500); $messages[] = $d; $d = dba_fetch("id_$i", $db); if($d === FALSE) resp("DB read failed", 500); $ids[] = $d; } $result = array('head' => $head, 'messages' => $messages, 'ids' => $ids); // Return the response encoded as json. header('Content-Type: application/json'); echo json_encode($result);