此 SQL 清理代码的说明

发布于 2024-08-27 23:29:23 字数 383 浏览 12 评论 0原文

我从登录表单教程中得到了这个:

function sanitize($securitystring) {
        $securitystring = @trim($str);
        if(get_magic_quotes_gpc()) {
            $securitystring = stripslashes($str);
        }
        return mysql_real_escape_string($securitystring);
    }

有人能准确解释一下它的作用吗?我知道“clean”变量随后会被调用来清理字段;即 $email = sanitize($_POST['email']);

I got this from for a login form tutorial:

function sanitize($securitystring) {
        $securitystring = @trim($str);
        if(get_magic_quotes_gpc()) {
            $securitystring = stripslashes($str);
        }
        return mysql_real_escape_string($securitystring);
    }

Could some one explain exactly what this does? I know that the 'clean' var is called up afterwards to sanitize the fields; I.e. $email = sanitize($_POST['email']);

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

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

发布评论

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

评论(5

ㄟ。诗瑗 2024-09-03 23:29:23

基本上,如果您打开了魔术引号,POST/SESSION 数据中的特殊字符将自动转义(与将addslashes() 应用于字符串相同)。 MySQL转义函数比PHP的addslashes()更好(尽管我不记得确切的原因)。

您的代码所做的是检查 php.ini 文件是否打开了魔术引号,如果是,则从数据中删除斜杠,然后使用 MySQL 函数重新对其进行清理。如果魔术引号未打开,则无需去除斜杠,因此数据只需使用 MySQL 函数进行清理并返回。

Basically, if you have magic quotes switched on, special characters in POST/SESSION data will automatically be escaped (same as applying addslashes() to the string). The MySQL escape functions are better than PHP's addslashes() (although I can't remember the exact reasons why).

What your code does is check if the php.ini file has magic quotes turned on, if so the slashes are stripped from the data and then it is re-sanitised using the MySQL function. If magic quotes is not on, there is no need to strip slashes so the data is just sanitised with the MySQL function and returned.

鼻尖触碰 2024-09-03 23:29:23

首先,这段代码是错误的。
它有错误的含义和错误的名称。

没有 SQL 数据准备代码进行任何清理或清理。
它只是在逃避。而且这种逃避必须是无条件的。
并且逃避不应该与其他任何事情混合在一起。

因此,它必须是三个独立的功能,而不是一个。

  1. 摆脱魔术引号。必须在数据输入时单独完成。
  2. 如果你愿意的话可以修剪一下。它只是文字美化,没有任何关键功能。
  3. mysql_real_escape_string() 为 SQL 查询准备数据。

所以,这里唯一与 mysql 相关的函数是 mysql_real_escape_string()。虽然它没有使数据变得“干净”,但只是转义分隔符。因此,此函数必须用于被视为字符串并用引号引起来的数据。所以,这是一个很好的例子:

$num=6;
$string='name';
$num=mysql_real_escape_string($num);
$string=mysql_real_escape_string($string);
$query="SELECT * FROM table WHERE name='$name' AND num='$num'";

虽然这个例子是错误的:

$num=6;
$string='name';
$num=mysql_real_escape_string($num);
$string=mysql_real_escape_string($string);
$query2="SELECT * FROM table WHERE name='$name' AND num=$num";

尽管 $query2 不会抛出语法错误,但这是错误的数据准备,并且 mysql_real_escape_string 在这里没有任何帮助。因此,此函数只能用于转义被视为字符串的数据。尽管可以对任何数据类型执行此操作,但有一些例外,例如 LIMIT 参数,不能将其视为字符串。

First of all, this code is wrong.
It has wrong meaning and wrong name.

No SQL data preparation code does any cleaning or sanitization.
It does merely escaping. And this escaping must be unconditional.
and escaping shouldn't be mixed with anything else.

So, it must be three separated functions, not one.

  1. Getting rid of magic quotes. Must be done separately at the data input.
  2. trim if you wish. It's just text beautifier, no critical function it does.
  3. mysql_real_escape_string() to prepare data for the SQL query.

So, the only mysql related function here is mysql_real_escape_string(). Though it makes no data "clean", but merely escape delimiters. Therefore, this function must be used only with data what considered as a string and enclosed in quotes. So, this is a good example:

$num=6;
$string='name';
$num=mysql_real_escape_string($num);
$string=mysql_real_escape_string($string);
$query="SELECT * FROM table WHERE name='$name' AND num='$num'";

while this example is wrong:

$num=6;
$string='name';
$num=mysql_real_escape_string($num);
$string=mysql_real_escape_string($string);
$query2="SELECT * FROM table WHERE name='$name' AND num=$num";

Even though $query2 would not throw a syntax error, this is wrong data preparation and mysql_real_escape_string would help nothing here. So, this function can be used only to escape data that treated as a string. though it can be done to any data type, there is some exceptions, such as LIMIT parameters, which cannot be treat as a strings.

苏佲洛 2024-09-03 23:29:23

trim() 删除所有空格,如果启用了魔术引号,则使用 stripslashes() 从任何转义引号中删除反斜杠。 mysql_real_escape_string() 准备好一个字符串,以便安全地在 mysql 查询中使用。

以下是所使用函数的文档: http://php.net/manual/en /function.trim.phphttp:// /php.net/manual/en/function.get-magic-quotes-gpc.php, http://php.net/manual/en/function.stripslashes.phphttp://php.net/manual/en/function.mysql-real-escape-string.php

trim() gets rid of all whitespace, and if magic quotes is on, the backslash is removed from any escaped quotes with stripslashes(). mysql_real_escape_string() readies a string to be used in a mysql query safely.

here are the docs for the functions used: http://php.net/manual/en/function.trim.php, http://php.net/manual/en/function.get-magic-quotes-gpc.php, http://php.net/manual/en/function.stripslashes.php, http://php.net/manual/en/function.mysql-real-escape-string.php

你的他你的她 2024-09-03 23:29:23

mysql_real_escape_string用于对字符串中的字符进行转义,为'等字符添加反斜杠,防止攻击者在字符串中嵌入额外的SQL语句。如果字符串未转义,则可以附加其他 SQL。例如,可能会执行类似的操作:

SELECT * FROM tbl WHERE col = 'test' ;从表中删除*; SELECT 'owned'

magic_quotes 确实对其自身进行了转义,尽管如果我没记错的话,现在不鼓励使用它。此外,MySQL 函数将执行所有需要的转义以防止 SQL 注入攻击。

mysql_real_escape_string is used to escape characters in the string to add backslashes to characters such as ', which prevents an attacker from embedding additional SQL statements into the string. If the string is not escaped, additional SQL can be appended. For example, something along the lines of this might be executed:

SELECT * FROM tbl WHERE col = 'test' ; DELETE * FROM tbl ; SELECT 'owned'

magic_quotes does escaping of its own, although if I remember correctly its use is now discouraged. Besides, the MySQL function will do all the escaping you need to prevent SQL injection attacks.

落在眉间の轻吻 2024-09-03 23:29:23

某些(旧)服务器启用了 magic_quotes。这意味着所有外部输入都会被更改以(据称)转义它,以便注入 MySQL 查询中。因此,O'Brian 变为 O\'Brian。这是 PHP 团队的一个早期设计决策,但事实证明是错误的:

  • 您并不总是需要将输入注入到数据库查询中
  • 并非所有数据库引擎都使用反斜杠作为转义字符
  • 使用反斜杠转义单引号是不够的,即使对于 MySQL 也是
  • 如此服务器安全依赖于可以禁用的 PHP 设置,

因此最好在没有 magic_quotes 的情况下进行编码。问题来自可再发行代码:您无法知道服务器是否启用或禁用 magic_quotes。因此,您可以使用 get_magic_quotes_gpc() 来检测它们是否打开,如果是这样,请使用 stripslashes() 来(尝试)恢复原始输入。

Some (old) servers have magic_quotes enabled. That means that all external input is altered to (supposedly) escape it in order to be injected in a MySQL query. So O'Brian becomes O\'Brian. This was an early design decision by the PHP team that proved wrong:

  • You don't always need to inject input into database queries
  • Not all DB engines use back slashes as escape char
  • Escaping single quotes with backs slashes is not enough, even for MySQL
  • Your server security relies on a PHP setting that can be disabled

So it's way better to code without magic_quotes. The problem comes with redistributable code: you cannot know if the server will have magic_quotes enabled or disabled. So you can use get_magic_quotes_gpc() to detect it they're on and, if so, use stripslashes() to (try to) recover the original input.

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