dongzhangji4824 2013-12-01 19:00
浏览 44
已采纳

我从db获取数据并在另一个查询中重用它们。 重新准备它们是多余的吗?

I always wondered if my behavior (ALWAYS preparing/escaping) is redundant or, for security reasons, always good to do. Here is an example:

FIRST STEP - A user insert some data

For instance, a user inserts a post. This post is saved, as written by the user, into the variable $post. Then i put it into DB

 $stmt = $dbh->prepare("INSERT INTO posts VALUES (:post)"); //simplified query
 $stmt->bindParam(":post", $post); //i prevent SQL injection
 $stmt->execute(); //run the query

As you can notice, i prevented SQL injection. Necessary, being $post written by users.

SECOND STEP - I fetch and reuse in another query

For a some reason i fetch that post

$stmt = $dbh->prepare("SELECT post FROM posts WHERE ..."); //simplified query
$stmt->execute();
$post=$stmt->fetch(PDO::FETCH_COLUMN,0);

Now, as before, i have the post saved into $post. The difference is, that post was taken from DB and not from user.

Here comes the question: if i'm gonna use the just fetched $post again in another query (INSERT, compare, UPDATE, etc...), should i prepare/escape it again? The logic says yes, because that value was escaped when it was inserted into DB, but being I unexpert about security, i wanna be sure...maybe by not escaping again, i'm giving some hackers a chance to attack my application!

//are these dangerous?
$stmt = $dbh->prepare("SELECT something FROM somewhere WHERE some_value=$post");
$stmt = $dbh->prepare("INSERT INTO somewhere VALUES ($post)");

//are these safe or simply redundant?
$stmt = $dbh->prepare("SELECT something FROM somewhere WHERE some_value=:post");
$stmt->bindParam(":post", $post); //i prevent SQL injection

$stmt = $dbh->prepare("INSERT INTO somewhere VALUES (:post)");
$stmt->bindParam(":post", $post); //i prevent SQL injection
  • 写回答

1条回答 默认 最新

  • douyi3307 2013-12-01 19:18
    关注

    I believe that prepared statements are the php version of query parameters. That being the case, security is not the only benefit.

    One other benefit is that special characters get escaped. The most common example is the apostrophe. You don't want apostrophe's being treated as single quotes. Bad things happen.

    Another benefit is better performance, especially on busy sites. When your db first sees an sql string it has to compile it and develop an execution plan. The compilation stays cached for awhile. If you run the same query with a different parameter, it will use the cached compilation. If you sent in a different string, it would have to compile again.

    Using query parameters, even when the data comes from your db, is always as good idea.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?