douce1368 2016-06-28 21:40
浏览 46

PHP和AJAX崩溃整个服务器


I'm attempting to modify and read a database using AJAX and PHP.
I have a PHP file that is accessed every 2 seconds through jQuery.
I use a GoDaddy web hosting service, if that makes a difference.

When I open the website in 2 pages, the code runs for a while before it returns "not found" errors in the console.
When this happens, the entire web server seems to reboot, taking a minute or two.

I've seen other questions and answers with the same problem, but they have a different cause, and their solution does not apply to me.

I have stripped out irrelevant code for the sake of a faster read, but if you beleive you are missing information, I will supply more or all code I use.

//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.

The javascript is fully able to read the database, and having two tabs open proves that they interact with each other. The problem is that they halt after several seconds, and refreshing the page shows that the server is down.
If you have a similar problem, I hope this solves or helps you figure it out.
If you need any more information, please comment nicely. Thanks!

  • 写回答

1条回答 默认 最新

  • dongwei7048 2016-06-28 22:17
    关注

    Obviously, your code as-written is extremely dangerous: you allow arbitrary SQL statements to be executed. You should be writing your code in such a way that the SQL statements are contained strictly within your PHP source, and all user-supplied data is inserted into them as parameters.

    Having said that: the only reasonable way that you could "bring a web-site to its knees" is if you are unwittingly requesting too much data at one time. (Or if there is some other bug in your program ...) First, there will be a delay while the computer dutifully tries to do what you ask. Then, GoDaddy's gonna kick you out, one way or the other. :-) You don't tell us if you're using shared-hosting or a dedicated-VM. All shared-hosting services impose strict limits, not only upon how much memory your site may consume, but also how long it may take to respond, since theirs is a shared resource.

    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法