如何防止站点遭受 SQL 注入

发布于 2024-10-07 11:39:00 字数 257 浏览 0 评论 0原文

我想使用 PHP 和 PHP 从头开始​​创建一个网站。 MYSQL采用MVC架构。但根据客户要求,该站点必须防止 SQL 注入和代码注入。

我需要执行哪些必要步骤。

或者

哪个是启动该网站的最佳框架,可以防止 SQL 注入和代码注入。

我没有要求这个框架来创建一个牢记的讨论。我只是想知道哪一个更好,以便我可以从专业人士的指导开始。

谢谢我提前...

I am in idea to start a site from scratch using PHP & MYSQL using MVC Architecture. But according to client requirement the site must be prevent from SQL INJECTION and 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.

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

尘世孤行 2024-10-14 11:39:00

到目前为止,防止 SQL 注入最可靠的方法是专门使用 mysqli 参数化查询而不是手动构建 SQL 语句。

它比 mysql_real_escape_string() 好得多,因为意外忘记使用它的风险较低。

为了防止服务器端代码注入,在任何情况下都不要使用 eval()

要防止 Javascript 注入,请使用 strip_tags() 在显示它或将其存储在数据库中之前。

By far the most reliable way to prevent SQL injection is to use mysqli parameterized queries exclusively instead of building SQL statements manually.

It's much better than mysql_real_escape_string() because the risk of accidentally forgetting to use it is lower.

To prevent server-side code injection, don't ever under any circumstances use eval()

To prevent Javascript injection, treat all user-generated content with strip_tags() before displaying it or storing it in the DB.

蓝海似她心 2024-10-14 11:39:00

这两个函数将帮助您:

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]);
        }
}

使用 escape() 函数将所有内容发送到数据库并使用 _INPUT() 抓取所有表单。您可以对每个 $_POST 或 $_GET 使用 _INPUT 函数,但布尔函数除外,如empty($_POST['name']) 或 isset($_POST['name']) 等。

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(). You can use _INPUT function for every $_POST or $_GET except on boolean functions like empty($_POST['name']) or isset($_POST['name']) etc.

月下客 2024-10-14 11:39:00

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

escape all input from $_GET[], $_POST[] etc with mysql_real_escape_string() right away.

笑饮青盏花 2024-10-14 11:39:00

在传递字符串参数来构建 SQL 语句时,使用 mysql_real_escape_string 。其中包括作为字符串处理的数字等参数。

Use mysql_real_escape_string when passing string parameters to build SQL statements. That includes an numeric, etc., parameters that you handle as strings.

遮了一弯 2024-10-14 11:39:00

使用 mysql_real_escape_string

use mysql_real_escape_string

野心澎湃 2024-10-14 11:39:00

最好的方法是使用准备好的语句,没有之一。这将使您不必自己手动编写代码并平衡一切。 PHP 的 mysqli 已内置此功能,并且是进入这个世界的良好第一步。请查看 PHP 手册页

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. Look at the PHP manual page on this.

画尸师 2024-10-14 11:39:00

在了解了此处给出的有关在将用户数据放入数据库之前转义用户数据的所有好技巧之后,您必须考虑问题的第二个方面。

转义输出

这意味着应用程序的每个输出都应该根据此输出格式的规则进行转义。
例如,当您回显 HTML 页面的某些内容时,您必须通过 htmlentites 对其进行转义,以便数据中包含的任何 HTMl 在输出中都不会是 html。
对于 csv 输出,您应该转义引号或逗号,对于 JSON,您也应该转义,等等,每个输出都有他的规则。

这个东西有时用于在数据库中存储有潜在危险的数据(js代码,Html代码),仅在插入之前防止SQL注入。然后确保在任何输出之前正确转义它。

另一种思考方式是在数据库存储之前阻止任何js或HTML代码,但您仍然应该转义输出(以防万一)。

谈论框架,您应该看看 Zend Framework 上的 Zend_Filter、Zend_Validate 以及视图上的转义函数。

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

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.
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.

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. 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).

Talking about frameworks you should get a look at Zend Framework on the Zend_Filter, Zend_Validate, and the escape functions on views.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文