PHP和AJAX崩溃整个服务器

时间:2016-06-28 21:40:48

标签: javascript php jquery ajax


我正在尝试使用AJAX和PHP修改和读取数据库 我有一个PHP文件,通过jQuery每2秒访问一次 我使用GoDaddy网络托管服务,如果这有所不同。

当我以2页打开网站时,代码会在控制台中返回“未找到”错误之前运行一段时间 发生这种情况时,整个Web服务器似乎重启,需要一两分钟
我已经看到了同样问题的其他问题和答案,但他们有不同的原因,他们的解决方案不适用于我。

为了更快的阅读,我删除了不相关的代码,但如果你相信你缺少信息,我将提供更多或所有我使用的代码。

//javascript/jQuery code
$(document).ready(function() {
    var id = Math.round(Math.random() * 7000000);
    var x = 0;
    var y = 0;// x and y are set to the mouse coordinates in code that I cut out.
    var db = [];

    //sends its own ID to add to the database
    $.post('src/insert.php?id=' + id + '&x=' + x + '&y=' + y);

    setInterval(function() {

        //reads the database, and sends its own ID for SQL to exclude
        $.get('src/select.php?id=' + id, function(data) {
            db = data;
            //db becomes an array of the database table in code that I cut out.
        });

        //sends its own data to the database through PHP
        $.post('src/update.php?id=' + id + '&x=' + x + '&y=' + y);

    }, 2000);//this code activates every 2 seconds.
});

//PHP code (I have 3 files like this, and I'll explain differences when necessary.)
$hostname = "123mysql123.example.com";
$username = "theuser";
$dbname = "thedatabase";
$password = "nospaces";

//I change this part from each file so it SELECTs, INSERTs, and UPDATEs.
//I've tested these and they work, but they might be crashing from overload.
$query = "SQL CODE WITH " . $_GET['data'] . " FROM GET";

$conn = mysqli_connect($hostname, $username, $password, $dbname);
if(!$conn) {
    die("error message");
}

//in files where no return is needed, the query is not stored to a variable.
$result = mysqli_query($conn, $query);

mysqli_close($conn);

//where a return value is needed, here I use mysqli_fetch_array($result) to get the array
//I have more code to put it into string form and echo it.
//the javascript parses it and uses it.

javascript完全能够读取数据库,并且打开两个选项卡证明它们彼此交互。问题是它们在几秒钟后停止,刷新页面显示服务器已关闭 如果您有类似的问题,我希望这可以解决或帮助您解决问题 如果您需要更多信息,请好好评论。谢谢!

1 个答案:

答案 0 :(得分:0)

显然,您编写的代码非常危险:您允许执行任意 SQL语句。您应该以这样的方式编写代码:SQL语句在PHP源代码中包含严格,并且所有用户提供的数据都作为参数插入到它们中。 < / p>

话虽如此:你唯一合理的方式可以让网站瘫痪&#34;如果您无意中一次请求过多数据。 (或者如果您的程序中还有其他错误 ...)首先,会在计算机尽职尽责地尝试按照您的要求进行延迟。 然后, GoDaddy会以某种方式将你踢出局。 : - )您没有告诉我们您是使用共享托管还是专用VM。所有共享托管服务都会严格限制,不仅限制您的网站可能消耗多少内存,还需要多长时间才能响应,因为它们是共享资源。