从表单/URL 清理 POST/GET 变量的最佳方法?

发布于 2024-07-20 07:05:53 字数 1106 浏览 8 评论 0原文

可能的重复:
在 PHP 中停止 SQL 注入的最佳方法

我正在创建一个使用 PHP 的网站,利用 MySQL 数据库并处理来自 URL 的表单和变量。 这些变量用于动态构造 SQL 查询字符串。 所以我需要一个强大的解决方案来确保没有人尝试 SQL 注入等。我的一个朋友说我实际上应该只使用存储过程来访问数据库,但这实际上并不可行,因为我使用的主机不不允许这些。

这是我正在使用的代码(它是包装数据库命令的类的一部分):

...
public function Sanitize($Variable)
{
    if(is_resource($this->ServerConnection))
    {
        $Variable = str_replace(";", "", $Variable);
        if(get_magic_quotes_gpc())
        {
            if(ini_get('magic_quotes_sybase'))
            {
                $Variable = str_replace("''", "'", $Variable);
            }
            else
            {
                $Variable = stripslashes($Variable);
            }
        }
        return mysql_real_escape_string($Variable, $this->ServerConnection);
    }
    else
    {
        $this->PrintError("The Sanitize function is not available as there is no server connection.");
    }
}
...

这个函数足够强大吗? 我应该做点别的吗?

Possible Duplicate:
Best way to stop SQL Injection in PHP

I am creating a website using PHP that makes use of a MySQL database and handles forms and variables from the URL. The variables are being using to dynamically construct SQL query strings. So i need a robust solution to make sure nobody is trying a SQL injection, etc.. A friend of mine has said that really i should only use stored procedures to access the database but that's not really feasible because the host i'm using doesn't allow these.

Here is the code i'm using (it's part of a class to wrap DB commands):

...
public function Sanitize($Variable)
{
    if(is_resource($this->ServerConnection))
    {
        $Variable = str_replace(";", "", $Variable);
        if(get_magic_quotes_gpc())
        {
            if(ini_get('magic_quotes_sybase'))
            {
                $Variable = str_replace("''", "'", $Variable);
            }
            else
            {
                $Variable = stripslashes($Variable);
            }
        }
        return mysql_real_escape_string($Variable, $this->ServerConnection);
    }
    else
    {
        $this->PrintError("The Sanitize function is not available as there is no server connection.");
    }
}
...

Is this function robust enough? Should i be doing anything else?

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

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

发布评论

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

评论(2

对风讲故事 2024-07-27 07:05:53

可能值得阅读这篇文章

Might be worth reading this post.

忆离笙 2024-07-27 07:05:53

最好的方法是什么......

没有最好的方法。 这取决于上下文。

.. 清理来自 .. 的 POST/GET 变量

认为数据好坏是一种有缺陷的思维模式。 数据只是数据。 它的使用环境决定了它是否是恶意的。 如果您在数据库服务器上直接执行某些单词,它们可能会很糟糕。 有些话如果给未成年人看是不好的。 这是关于上下文的。

What is the best way of ...

There is no best way. It depends on the context.

.. sanitising POST/GET variables from ..

It is a flawed mode of thinking that data are good or bad. Data is just data. It's the context in which it's used that makes it malicious or not. Some words may be bad if you execute them unadorned on a database server. Some words are bad if you display them to minors. It's about context.

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