我正在处理一个在MySQL中使用MySQL的项目,我不知道什么时候应该使用tep_db_input()还是tep_db_prepare_input()。我假设我应该在插入/更新的任何字符串周围使用tep_db_input(),但是什么时候应该使用其他函数呢?
例如,如果我要从数据库中选择一些数据,然后使用结果将一行插入到另一个表中,那么是否需要在某个时候准备输入?还是再次使用tep_db_input?
$width = '3"'; // 3 inches
$new_height = '3\' 5"'; // 3 feet 5 inches
$result = tep_db_query(
"SELECT height
FROM measurements
WHERE width = '".tep_db_input($width)."'"
);
while ($row = tep_db_fetch_array($result)) {
tep_db_query(
"INSERT INTO measurement_history (
field,
old_value,
new_value
) VALUES (
'height',
'".tep_db_input($row['height'])."',
'".tep_db_input($new_height)."'
)"
);
}这是正确的吗?
编辑::如果有人不熟悉这些函数,下面是它们的定义:
function tep_sanitize_string($string) {
$patterns = array ('/ +/','/[<>]/');
$replace = array (' ', '_');
return preg_replace($patterns, $replace, trim($string));
}
function tep_db_input($string, $link = 'db_link') {
global $$link;
if (function_exists('mysql_real_escape_string')) {
return mysql_real_escape_string($string, $$link);
} elseif (function_exists('mysql_escape_string')) {
return mysql_escape_string($string);
}
return addslashes($string);
}
function tep_db_prepare_input($string) {
if (is_string($string)) {
return trim(tep_sanitize_string(stripslashes($string)));
} elseif (is_array($string)) {
reset($string);
while (list($key, $value) = each($string)) {
$string[$key] = tep_db_prepare_input($value);
}
return $string;
} else {
return $string;
}
}发布于 2013-05-17 08:16:08
tep_db_input使用字符串或字符串,这是准备数据库输入的推荐方法。(我猜这个函数将在以后的版本中使用mysqli_real_escape_string()或similiar,因为mysql_real_escape_string将从PHP5.5.0开始被弃用。)
其中,tep_db_input和mysql_real_escape_string只进行转义:
mysql_real_escape_string() calls MySQL's library function mysql_real_escape_string,
which prepends backslashes to the following characters: \x00, \n, \r, \, ', " and \x1a.tep_db_prepare_input做了不同的事情,比如裁剪白空间,替换括号和取消引用(!)通过调用条纹斜杠。
因此,我的建议是:始终使用tep_db_input。如果您使用tep_db_prepare_input来去除空格等,那么之后也要使用tep_db_input。
发布于 2014-06-29 21:18:04
这有点奇怪,但你两者都用。这样做将防止来自恶意用户的攻击,以及来自不寻常输入的意外问题。
对来自HTML的任何输入数据使用tep_db_prepare输入。这就清除了HTML、魔术引号和脚本注入的问题。不要在从数据库中检索到的文本上使用此方法。
然后,在将其写入数据库之前使用tep_db_input。这将转义MySQL字符,以防止SQL注入攻击和其他此类问题。
下面是一个代码示例,显示它:
$clean = tep_db_prepare_input($_POST['name']);
$query_text = tep_db_query("select * from " . TABLE_NAME . " where name='" . tep_db_input($clean) . "'");https://stackoverflow.com/questions/16598376
复制相似问题