如何防止我的代码和 sql 架构受到 sql 注入(inject)......??
我正在使用 Php 进行编码......
我有一些登录表格。我看了一些sql注入(inject)查询
string sql = "SELECT * FROM table_name WHERE smth='" + UserInput + "'";
ExecuteSql(sql);
SELECT fieldlist
FROM table
WHERE field = 'steve@unixwiz.net'';
我知道 sql 注入(inject)是由于我们错误的编码和从数据库检索数据的错误查询而发生的。我做了一些谷歌以从 sql 注入(inject)器中保护我的项目,但我没有得到一些正确的信息。
我也使用 cakephp 框架进行编码,我知道在 cakephp 中,sql 注入(inject)非常困难,因为它会清理查询..这是真的吗??
我得到了这个信息:--
CakePHP already protects you against SQL Injection if you use CakePHP's ORM methods (such as find() and save()) and proper array notation (ie. array('field' => $value)) instead of raw SQL. For sanitization against XSS its generally better to save raw HTML in database without modification and sanitize at the time of output/display.
有人会指导我吗??
谢谢!!
最佳答案
@Learner 专门讨论 CakePHP 框架中的查询,而不是一般的注入(inject)(尽管了解这一点也很重要。
当 CakePHP 说
CakePHP already protects you against SQL Injection if you use CakePHP's ORM methods (such as find() and save()) and proper array notation (ie. array('field' => $value)) instead of raw SQL. For sanitization against XSS its generally better to save raw HTML in database without modification and sanitize at the time of output/display.
您需要做的就是它所说的。当您对数据库运行任意查询时,它不会保护您 - 它不能,因为您正在绕过 CakePHP。当它确实保护您时,是通过框架运行的查询:
$result = $this->Article->find(
'first', array(
'conditions' => array('Article.id' => $articleIDtoFind)));
在此示例中,$articleIDtoFind
来自何处并不重要,查询是安全的。 (如果它充满垃圾,它可能无法正常工作)
关于php - 需要一些重要提示 :- Sql injection for different queries. 。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10680494/