简体   繁体   English

mysql_query的参数使用

[英]Use of parameters for mysql_query

somewhere while studying I juz found out something interesting.. It says something as follows: 我在学习的某个地方发现了一些有趣的东西。它说如下:

$query = sprintf("SELECT firstname, lastname, address, age FROM friends 
WHERE firstname='%s' AND lastname='%s'",mysql_real_escape_string($firstname),
    mysql_real_escape_string($lastname));

using the query like this instead of 使用这样的查询而不是

$query="select firstname, lastname, address, age FROM friends
WHERE firstname='".$_RETURN['name1']."', lastname='".$_RETURN['name2']."'";

does this seem reasonable.. have u tried this coding ever.. and how it helps prevent any malicious attacks.. 这似乎合理吗..您是否曾经尝试过这种编码..以及它如何帮助防止任何恶意攻击..

First off, what this is about is called is SQL-Injection . 首先,这就是所谓的SQL-Injection It's basically just the possibility to alter queries against the database via user input. 基本上,这只是通过用户输入更改对数据库的查询的可能性。

Let's look at an example: 让我们看一个例子:

Query: 查询:

SELECT temp1 FROM temp WHERE temp2 = 'VAR1';

Now we'll assign VAR1 the value of: '; DROP TABLE *; -- 现在,我们为VAR1分配以下值: '; DROP TABLE *; -- '; DROP TABLE *; -- '; DROP TABLE *; -- And we'll get: '; DROP TABLE *; --然后我们会得到:

SELECT temp1 FROM temp WHERE temp2 = ''; DROP TABLE *; --';

With mysql_real_escape_string it would look like this: 使用mysql_real_escape_string它看起来像这样:

SELECT temp1 FROM temp WHERE temp2 = '\'; DROP TABLE *; --'

mysql_real_escape_string 'secures' a string for usage within a query. mysql_real_escape_string '保护'字符串以在查询中使用。

But in the end, you should stop using the mysql_* altogether . 但是最后, 您应该完全停止使用mysql_* They're deprecated and considered as insecure when it comes to preventing SQL injection or other means of tempering with the queries. 在防止SQL注入或对查询进行调整的其他方式上,它们已被弃用并被认为是不安全的。

You should simply stop concatenating queries together like this and start using prepared statements , which not only are easier to use, prevent SQL Injection by default but also can improve the speed of your application. 您应该简单地停止像这样的串联查询,并开始使用准备好的语句 ,这不仅更易于使用,默认情况下防止SQL注入,而且还可以提高应用程序的速度。

For PHP there are two extensions which are designed to close the whole mysql_* opened: 对于PHP,有两个扩展名旨在关闭打开的整个mysql_*

And I say it again: Please stop using mysql_* ! 我再说一遍: 请停止使用mysql_*

Using formatting functions like sprintf is purely a matter of taste; 使用诸如sprintf类的格式化功能纯粹是个问题。 the big advantage in the first example is that the function mysql_real_escape_string prevents all SQL injections (explained in one of the other answers); 第一个示例的最大优势在于,函数mysql_real_escape_string阻止了所有SQL注入(在其他答案中作了解释); unlike the somewhat iffy magic_quotes_gpc feature in PHP, which many people rely on instead. 不像PHP中有些难以理解的magic_quotes_gpc功能,很多人依赖它。

magic_quotes_gpc automatically escapes things you receive in requests from clients... but it cannot detect so-called second-level injections: magic_quotes_gpc自动转义您从客户端请求中收到的内容...但是它无法检测到所谓的二级注入:

  1. You get a malicious query from a client and store its contents in the database. 您从客户端收到恶意查询,并将其内容存储在数据库中。 magic_quotes_gpc prevents SQL injection; magic_quotes_gpc防止SQL注入; the malicious string gets stored correctly. 恶意字符串会正确存储。
  2. Later on, you fetch this string from the database and include it in another query. 稍后,您从数据库中获取此字符串,并将其包含在另一个查询中。 Now the string didn't come out of a request, so magic_quotes_gpc doesn't escape the string. 现在该字符串不是来自请求的,因此magic_quotes_gpc不会转义该字符串。 Voilà, SQL injection; Voilà,SQL注入; your data is now probably gone. 您的数据现在可能消失了。

Using some means of escaping yourself, either something like mysql_real_escape_string or a database abstraction layer with a query builder (eg Adodb), is definitely superior to just hoping for the best. 使用某种逃避自我的方法,例如mysql_real_escape_string或带有查询生成器的数据库抽象层(例如Adodb),绝对比仅仅希望达到最佳效果更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM