Php 魔术报价是在&&;stripslashes和mysql real escape字符串一起使用
可能重复:Php 魔术报价是在&&;stripslashes和mysql real escape字符串一起使用,php,mysql,mysql-real-escape-string,stripslashes,magic-quotes-gpc,Php,Mysql,Mysql Real Escape String,Stripslashes,Magic Quotes Gpc,可能重复: 我在一个免费的php服务器上,我没有访问php.ini文件的权限,magic_quotes正在运行。因此,当我通过表单页面向mysql添加记录时,我的php代码是 我的问题: 我想消除魔法效果,因为它会贬值并删除和潜在的恶意用户 以最佳方式输入mysql。我上面的代码是正确的还是需要更改, 改进? 谢谢 只需stripslashes()就足以摆脱神奇的引号 $text = stripslashes($_POST["text"]); 请参阅在运行时删除魔术引号的更完整示例: 你
我在一个免费的php服务器上,我没有访问php.ini文件的权限,magic_quotes正在运行。因此,当我通过表单页面向mysql添加记录时,我的php代码是
我的问题:谢谢
$text = stripslashes($_POST["text"]);
请参阅在运行时删除魔术引号的更完整示例:
你真的应该得到一个不同的PHP服务器。自PHP5.3.0(2009年6月)以来,Magic quotes一直被弃用。您的PHP托管站点已经有四年没有更新PHP了,您还面临着许多其他错误甚至安全漏洞的风险。现在是你转移到另一个主机的时候了
您的评论如下: 是的,stripslashes只是将请求参数转换为纯文本 至于是否应该使用mysql\u real\u escape\u string()的问题 首先,只有在将值插值到SQL查询中时,才应该这样做。您不一定要对每个POST值都这样做,因此将转义应用于所有内容是愚蠢的 打个比方,这就像是在你知道自己要吃多少东西和吃剩多少东西之前,把晚餐放进冰箱的储藏容器里一样。:-) 其次,您不应该再使用mysql_*函数了。从PHP5.5.0开始,它们将在PHP的未来版本中删除。现在应该开始使用mysqli_*或PDO函数 第三,在SQL查询中不应该对动态值使用转义。相反,使用带参数的准备好的查询。参数化查询比使用mysql\u real\u escape\u string()更安全、更易于编码、运行速度更快
下面是你的评论: 不,我想你还没有拿到 如果要在SQL查询中插入$_POST[“text”],并且启用了magic quotes,请执行以下操作:
// remove the magic quotes simply with stripslashes():
$text = stripslashes($_POST["text"]);
// prepare an SQL statement, using a ? placeholder instead of interpolated value
$stmt = $mysqli->prepare("INSERT INTO mytable (mytext) VALUES (?)");
// always check for an error on prepare, you might have made a syntax error,
// or the table might not exist, etc.
if ($stmt === false) {
die($mysqli->error);
}
// bind one PHP variables for each parameter placeholder in the query
$stmt->bind_param("s", $text);
// then execute! MySQL will use the values of the PHP variables you bound
// in place of the placeholders
$status = $stmt->execute();
// always check for an error on execute too, because the value of the parameter
// might cause the query to fail, e.g. conflicting with another value in a
// unique column, etc.
if ($status === false) {
die($stmt->error);
}
如果使用查询参数,则不需要使用mysqli\u real\u escape\u string()
如果您需要更多帮助,这里有一个关于mysqli的教程,其中包含显示绑定参数的示例:
- 只需stripslashes()就足以摆脱神奇的引号
$text = stripslashes($_POST["text"]);
请参阅在运行时删除魔术引号的更完整示例:
你真的应该得到一个不同的PHP服务器。自PHP5.3.0(2009年6月)以来,Magic quotes一直被弃用。您的PHP托管站点已经有四年没有更新PHP了,您还面临着许多其他错误甚至安全漏洞的风险。现在是你转移到另一个主机的时候了
您的评论如下: 是的,stripslashes只是将请求参数转换为纯文本 至于是否应该使用mysql\u real\u escape\u string()的问题 首先,只有在将值插值到SQL查询中时,才应该这样做。您不一定要对每个POST值都这样做,因此将转义应用于所有内容是愚蠢的 打个比方,这就像是在你知道自己要吃多少东西和吃剩多少东西之前,把晚餐放进冰箱的储藏容器里一样。:-) 其次,您不应该再使用mysql_*函数了。从PHP5.5.0开始,它们将在PHP的未来版本中删除。现在应该开始使用mysqli_*或PDO函数 第三,在SQL查询中不应该对动态值使用转义。相反,使用带参数的准备好的查询。参数化查询比使用mysql\u real\u escape\u string()更安全、更易于编码、运行速度更快
下面是你的评论: 不,我想你还没有拿到 如果要在SQL查询中插入$_POST[“text”],并且启用了magic quotes,请执行以下操作:
// remove the magic quotes simply with stripslashes():
$text = stripslashes($_POST["text"]);
// prepare an SQL statement, using a ? placeholder instead of interpolated value
$stmt = $mysqli->prepare("INSERT INTO mytable (mytext) VALUES (?)");
// always check for an error on prepare, you might have made a syntax error,
// or the table might not exist, etc.
if ($stmt === false) {
die($mysqli->error);
}
// bind one PHP variables for each parameter placeholder in the query
$stmt->bind_param("s", $text);
// then execute! MySQL will use the values of the PHP variables you bound
// in place of the placeholders
$status = $stmt->execute();
// always check for an error on execute too, because the value of the parameter
// might cause the query to fail, e.g. conflicting with another value in a
// unique column, etc.
if ($status === false) {
die($stmt->error);
}
如果使用查询参数,则不需要使用mysqli\u real\u escape\u string()
如果您需要更多帮助,这里有一个关于mysqli的教程,其中包含显示绑定参数的示例: