Search code examples
phpajaxhostingshared-hosting

Repetitive Notif Checking triggers Error 508 (Loop Detected)


I call an AJAX to check DB if there is new notif every 3 or 10 seconds with the same query from 4 different browsers at the same time. But at some point after loop 100+, the server returns Error 508 (Loop Detected). This is just simple site so I don't think I need VPS server.

I added timestamp in SELECT as query differentiator, put unset, flush, mysqli_free_result, pause, mysqli_kill, mysqli_close, but error still occurs. Entry Processes hit 20/20.

Script

var counter = 1;
var notiftimer;

$(document).ready(function() {
    ajax_loadnotifs();
});

function ajax_loadnotifs() {
    $.ajax({
        type: "post",
        url: "service.php",
        dataType: "json",
        data: { action:'loadnotifs' },
        success: function(data, textStatus, jqXHR){
            $("div").append($("<p>").text(counter++ + ": succeeded"));

            notiftimer = setTimeout(function() {
                ajax_loadnotifs();
            }, 3000);
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log(jqXHR.responseText);
        }
    });
}

service.php

$link = mysqli_connect('localhost', 'root', 'root', 'testdb');
$notifs = array();

$query = "SELECT id, message FROM notifs LIMIT 20";
if (!$temp_notifs = mysqli_query($link, $query)) {
    die(json_encode(array("errmsg" => "Selecting notifs.")));
}

while($notif = mysqli_fetch_assoc($temp_notifs)) {
    $notifs[] = $notif;
}

mysqli_close($link);        
echo json_encode($notifs);

cPanel - Resource Usage Overview

cPanel - Resource Usage Overview

When Entry Processes hits 20/20, I get Error 508. How to maintain low server Entry Processes? (Tested with 4 different browsers, run them all until loop 100+ on shared hosting. No issue on local computer)


Solution

  • Turns out that using https instead of http and AJAX 'get' method instead of 'post' prevent this error.