简体   繁体   English

如何防止SQL INJECTION中的站点

[英]How to Prevent a site from SQL INJECTION

I am in idea to start a site from scratch using PHP & MYSQL using MVC Architecture. 我想使用MVC架构使用PHP和MYSQL从头开始创建一个站点。 But according to client requirement the site must be prevent from SQL INJECTION and CODE INJECTION. 但根据客户要求,必须防止SQL INJECTION和CODE INJECTION。

What are the necessary steps i need to do. 我需要做什么必要的步骤。

OR 要么

Which is the best framework to start the site which prevents from SQL INJECTION and CODE INJECTION. 哪个是启动该站点的最佳框架,可防止SQL INJECTION和CODE INJECTION。

I didn't ask this to create a discussion with keeping in mind. 我并没有要求这个创建一个牢记的讨论。 I just want to know which one is better so that i can start with professionals guidance. 我只是想知道哪一个更好,以便我可以从专业人士的指导开始。

thanks i advance... 谢谢我提前...

By far the most reliable way to prevent SQL injection is to use mysqli parameterized queries exclusively instead of building SQL statements manually. 到目前为止,防止SQL注入的最可靠方法是独占使用mysqli参数化查询,而不是手动构建SQL语句。

It's much better than mysql_real_escape_string() because the risk of accidentally forgetting to use it is lower. 它比mysql_real_escape_string()好得多,因为意外忘记使用它的风险较低。

To prevent server-side code injection, don't ever under any circumstances use eval() 为了防止服务器端代码注入,在任何情况下都不要使用eval()

To prevent Javascript injection, treat all user-generated content with strip_tags() before displaying it or storing it in the DB. 要防止Javascript注入,请在显示或将其存储在数据库中之前,使用strip_tags()处理所有用户生成的内容。

使用mysql_real_escape_string()立即转义来自$_GET[]$_POST[]等的所有输入。

These two functions will help you: 这两个功能将帮助您:

function escape($escape)
{
    $escape = mysql_real_escape_string($escape) ;
    return $escape ;
}

function _INPUT($name)
{
    if ($_SERVER['REQUEST_METHOD'] == 'GET') {
        return strip_tags($_GET[$name]);
        }
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        return strip_tags($_POST[$name]);
        }
}

Send everything with escape() function to db and grab all forms with _INPUT(). 使用escape()函数将所有内容发送到db并使用_INPUT()获取所有表单。 You can use _INPUT function for every $_POST or $_GET except on boolean functions like empty($_POST['name']) or isset($_POST['name']) etc. 你可以为每个$ _POST或$ _GET使用_INPUT函数,除了布尔函数,如空($ _ POST ['name'])或isset($ _ POST ['name'])等。

Use mysql_real_escape_string when passing string parameters to build SQL statements. 传递字符串参数以构建SQL语句时使用mysql_real_escape_string That includes an numeric, etc., parameters that you handle as strings. 这包括您作为字符串处理的数字等参数。

使用mysql_real_escape_string

The best way to do this is to use prepared statements, bar none. 执行此操作的最佳方法是使用预准备语句,禁止无。 This will keep you from having to hand code and balance everything on your own. 这将使您不必自己手动编码和平衡所有内容。 PHP's mysqli has this built into it and is a good first step into this world. PHP的mysqli内置了它,是迈向这个世界的良好的第一步。 Look at the PHP manual page on this. 请看PHP的手册页

After all the good tips given here on escaping your user data before putting it in database, you must as weel think at the second side of the problem. 在将所有用户数据放入数据库之前提供的所有好提示之后,您必须在问题的第二方面思考。

Escape the ouptuts 逃离ouptuts

That means every output of your application should be escaped based on the rule of this output format. 这意味着应根据此输出格式的规则对应用程序的每个输出进行转义。 For example, when you echo something for an HTML page you must escape it via htmlentites, so that any HTMl contined in your data will not be html in the output. 例如,当您为HTML页面回显某些内容时,必须通过htmlentites对其进行转义,以便数据中包含的任何HTMl在输出中都不会是html。 And for a csv output you should escape quotes or commas, for JSON you should escape things as well, etc every output has his rules. 对于csv输出你应该转义引号或逗号,对于JSON你也应该逃避事情,等等每个输出都有他的规则。

This thing is sometime used to store data which is potentially dangerous (js code, Html code) in the DB, by only preventing SQL injection before insertion. 这个东西有时用于存储数据库中存在潜在危险的数据(js代码,Html代码),只是在插入之前阻止SQL注入。 And then ensuring it is properly escaped before any output. 然后确保在输出之前正确转义它。

Another way of thinking is to prevent any js or HTML code before the database storage, but you should still escape the output (in case of). 另一种思维方式是在数据库存储之前阻止任何js或HTML代码,但是你仍然应该转义输出(如果是)。

Talking about frameworks you should get a look at Zend Framework on the Zend_Filter, Zend_Validate, and the escape functions on views. 在讨论框架时,您应该查看Zend_Filter,Zend_Validate上的Zend Framework以及视图上的转义函数。

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

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