
"function checkTurn() { if (polling === false) { return; } request = $.ajax({ type: "GET", url: "battleship-checkTurn.php", data: { denBrukeren: denBrukeren, id: gameId, }, async: true, cache: false, timeout: 20000, success: function(data) { console.log('Polling = true'); if (polling === true) { if (data === denBrukeren) { polling = false; loadGame(); } } }, error: function(XMLHttpRequest, textStatus, errorThrown) { console.log('Reached timeout'); }, }); } $(document).ready(function() { setInterval(function() { checkTurn(); }, 20600); }); function abortRequest() { if (request) { request.abort(); } }"
"<?php require_once '../includes/db-inc.php'; $gameId = $_GET['id']; $denBrukeren = $_GET['denBrukeren']; $exitLoop = false; $returnValue = 'No set value'; while ($exitLoop === false) { $sql = "select * from battleship WHERE id = '$gameId';"; $result = mysqli_query($conn, $sql); $row = mysqli_fetch_assoc($result); $turnPlay = $row['turn']; if ($turnPlay === $denBrukeren) { $returnValue = $turnPlay; break; } sleep(1); } echo $returnValue;"
The JavaScript schedules checkTurn every 20600 ms and issues a jQuery AJAX GET with timeout 20000 ms when polling is true. The client code defines abortRequest but does not call it on page unload, and setInterval continues creating requests. The PHP script runs an endless while loop that queries the database and sleeps(1) until the turn matches, with no server-side timeout or client-connection check. The persistent server loop and un-aborted requests can leave connections hanging and cause the interface to freeze. The PHP also contains an unescaped query string and a semicolon inside the SQL string.
Read at SitePoint Forums | Web Development & Design Community
Unable to calculate read time
Collection
[
|
...
]