我知道以任何一种方式添加它都没有坏处,但我很好奇......
如果我要使用 htmlentities();用 ENT_QUOTES 然后用 mysql_real_escape_string();在将变量输入数据库之前,只需使用 html_entity_decode();连同 stripslashes();显示信息...
这仍然安全可靠吗?
最佳答案
在将数据存储到数据库之前,您不需要使用 htmlentities。事实上,如果你不这样做,以后事情会变得更容易。当您在 HTML 输出中回显字符串时,仅在字符串上使用 htmlentities(无论您是从数据库还是从其他来源获取字符串)。
从数据库中获取数据后,您无需对数据应用斜杠。数据库没有存储额外的转义字符——除非你错误地应用了双重转义。
这是正确的顺序:
从表单中获取数据
$input = $_GET["input"];
应用转义一次。
$quoted_input = "'" . mysql_real_escape_string($input) . "'";
将其插入数据库
$sql = "INSERT INTO MyTable (column1) VALUES ($quoted_input)"; $success = mysql_query($sql);
稍后从数据库中获取它
$sql = "SELECT column1 FROM MyTable"; $result = mysql_query($sql); $row = mysql_fetch_assoc($result); $data = $row["column1"];
在输出时应用 htmlentities 一次。
echo htmlentities($data);
关于php - 如果我使用以下内容,正则表达式是否用于表单验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3761542/