Php 减少请求并优化动态调用
我在one.com上托管,最近遇到了一个问题,由于来自同一IP地址的请求太多,用户被阻止访问我的网站。我认为原因是我调用数据库时编程笨拙。我在do while循环中动态调用了几个连接,如图所示:Php 减少请求并优化动态调用,php,mysql,query-optimization,Php,Mysql,Query Optimization,我在one.com上托管,最近遇到了一个问题,由于来自同一IP地址的请求太多,用户被阻止访问我的网站。我认为原因是我调用数据库时编程笨拙。我在do while循环中动态调用了几个连接,如图所示: if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (P
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$unit_nr_var = $unit_nr_var1.'.';
$rs_nr = 1;
$te_unit_id_var = $unit_nr_var.$rs_nr;
mysql_select_db($database_db_stat, $db_stat);
$query_rs_theory = sprintf("SELECT * FROM tbl_exercise_theory WHERE tet_exercise_id = %s", GetSQLValueString($unit_nr_var1, "text"));
$rs_theory = mysql_query($query_rs_theory, $db_stat) or die(mysql_error());
$row_rs_theory = mysql_fetch_assoc($rs_theory);
$totalRows_rs_theory = mysql_num_rows($rs_theory);
for($i = 0; $i <= 9; $i++) {
$_cook_id = $_COOKIE['MM_cookieUserID'];
mysql_select_db($database_db_stat, $db_stat);
$query_rs_unit_[$i] = sprintf("SELECT te.*, tp.tp_answer FROM tbl_exercise te LEFT OUTER JOIN tbl_progression tp ON (tp.tp_user_id = %s AND tp.tp_exe_id = te.te_id) WHERE te_unit_id = %s ORDER BY te_id ASC", GetSQLValueString($_cook_id, "text"), GetSQLValueString($te_unit_id_var, "text"));
$rs_unit_[$i] = mysql_query($query_rs_unit_[$i], $db_stat) or die(mysql_error());
$row_rs_unit_[$i] = mysql_fetch_assoc($rs_unit_[$i]);
$totalRows_rs_unit_[$i] = mysql_num_rows($rs_unit_[$i]);
mysql_select_db($database_db_stat, $db_stat);
$query_rs_name_[$i] = sprintf("SELECT * FROM tbl_exercise_name WHERE ten_exercise_id = %s", GetSQLValueString($te_unit_id_var, "text"));
$rs_name_[$i] = mysql_query($query_rs_name_[$i], $db_stat) or die(mysql_error());
$row_rs_name_[$i] = mysql_fetch_assoc($rs_name_[$i]);
$totalRows_rs_name_[$i] = mysql_num_rows($rs_name_[$i]);
mysql_select_db($database_db_stat, $db_stat);
$query_Recordset[$i] = sprintf("SELECT * FROM tbl_exercise WHERE te_unit_id = %s ORDER BY te_id ASC", GetSQLValueString($te_unit_id_var, "text"));
$Recordset[$i] = mysql_query($query_Recordset[$i], $db_stat) or die(mysql_error());
$row_Recordset[$i] = mysql_fetch_assoc($Recordset[$i]);
$totalRows_Recordset[$i] = mysql_num_rows($Recordset[$i]);
$rs_nr = $rs_nr + 1;
$te_unit_id_var = $unit_nr_var.$rs_nr;
if (($totalRows_Recordset[$i] <= 0))
{
$i = 10;
}
}
我知道,这可能不是一种有效的方法来获取可变数量的表,这取决于页面变量。接下来的问题是:我该如何对此进行优化?因为来自同一ip的许多请求听起来像是您的托管提供商正在做的事情,而不是您的代码。即使你的代码写得很慢,用户仍然会得到响应,他们可能会花很长时间,或者可能会超时。是的,我的主机提供商正在这么做,但我想我的代码中可能有某种东西触发了它。这样我就可以优化我的代码并解决它。我有点怀疑这一点。这意味着来自同一ip的请求太多,这可能意味着他们试图抵御DDOS攻击。你可能想和他们的技术支持谈谈。您的代码有点难看,您在准备字符串值方面投入了大量的工作,但由于没有做显而易见的事情,并且暴露了sql注入漏洞,这是值得称赞的。为什么不使用像PDO这样的抽象呢?我正试图为每个页面准备大约5-8个数组,具体取决于GET值。另外,代码的其余部分也有点难看:-感谢链接的任何人,我会尝试阅读它并相应地进行更改。我不是要求你为我做那些会让它失去乐趣的工作,但我可以在哪里开始使用一个提示。我从来没有与PDO合作过,因此在这里超出了我的深度。