PHP mysql_real_escape_string() - > stripslashes

2019-07-21 00:34发布

我有问题逃逸/使用PHP / MySQL的剥离串 - 总是有似乎是多余的斜杠。


让我们以下面的字符串作为一个例子:

<span style="text-decoration:underline;">underline</span>


当添加一个字符串到数据库中,我与逃避它mysql_real_escape_string()和下面被存储在数据库中( 编辑 :直接使用MySQL的应用程序查询数据库检查该):

<span style=\\\"text-decoration:underline;\\\">underline</span>


当回读出的数据库,我传递字符串经过stripslashes() ,返回以下内容:

<span style=\"text-decoration:underline;\">underline</span>


由于报价仍逃脱,它打破了HTML和文本没有下划线。


  1. 为什么mysql_real_escape_string()加三个斜杠和stripslashes()中除去两个斜线? 我希望他们都添加/删除一个斜杠。
  2. 我怎样才能防止这种情况发生?
  3. 难道我处理这个正确的方式?

Answer 1:

Best Solution

In your php.ini file, odds are that the magic_quotes_gpc directive is set to on. This should be disabled for security reasons. If you don't have access to the php.ini file (eg. on a shared host), you can always accomplish the same using an .htaccess directive (assuming this is an apache server).

In your php.ini

magic_quotes_gpc Off

In an .htaccess file:

php_flag magic_quotes_gpc Off

Why is this happening?

The reason this is happening is due to the following course of logic.

  1. A string that needs escaping is sent to the server.
    • This is my string. It's awesome.
  2. Magic Quotes escapes the apostrophe before it gets to your code.
    • This is my string. It\'s awesome
  3. mysql_real_escape_string now has two characters to escape, the backslash \\ as well as the apostrophe \'.
    • This is my string. It\\\'s awesome
  4. This new super-escaped string is stored in the database.
  5. When the string is retrieved from the database, it get's passed to stripslashes. This removes the two escapes added in step 3, but since one of the backslashes has been escaped stripslashes thinks it belongs.
    • This is my string. It\'s awesome

This problem can really get out of hand when you re-submit these strings to the database, as each time the number of backslashes multiplies.

Alternative Solution

A quick-and easy alternative would be to simply remove the slashes added by magic_quotes before passing the string to mysql_real_escape_string.

$str = stripslashes($_POST['str']);
$str = mysql_real_escape_string($str);


Answer 2:

当添加一个字符串到数据库中,我与逃避它mysql_real_escape_string()和被存储在数据库中执行以下操作:

<span style=\\\"text-decoration:underline;\\\">underline</span>

不,这不对。 当你逃避一个SQL查询字符串,它只是在查询中传输数据。 数据库分析查询和存储在数据库中的数据,没有任何多余的斜线。 因此,当你从数据库中检索数据时,你应该取消转义任何东西。 这是一个常见的误解。

如果您发现有多余的斜杠输出,你可能有魔术引号打开。 将其关闭 。

编辑:

mysql> create table foo (bar text) ;
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO foo (bar) VALUES ("<span style=\\\"text-decoration:underline;\\\">underline</span>");
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM foo;
+-------------------------------------------------------------+
| bar                                                         |
+-------------------------------------------------------------+
| <span style=\"text-decoration:underline;\">underline</span> | 
+-------------------------------------------------------------+
1 row in set (0.00 sec)

正如你所看到的,查询比出现在数据库中的数据和查询它时,它因此怎么弄出来有逃避的一个多水平。 对你来说,什么可能是怎么回事,是你有魔术引号打开,然后在查询中嵌入在他们面前逃跑字符串。 这将导致双转义,篡改数据。 正确的解决办法是保持你做转义字符串,但关闭魔术引号。 而且,因为它出来的数据库进行数据的任何东西。 要注意的是数据已经在系统中需要首先清理。



Answer 3:

如果get_magic_quotes_gpc()是关闭的服务器,所以只有我们可以使用

$data= mysql_real_escape_string($_POST['data']);

如果get_magic_quotes_gpc()是在服务器,我们必须使用

$data= mysql_real_escape_string(stripslashes($_POST['data']));

否则你的数据添加两个反斜杠。

还另一种解决方案是,我们可以使用stripslashes($data) ,而从获取datadase如果我们使用只使用mysql_real_escape_string($_POST['data']);



文章来源: PHP mysql_real_escape_string() -> stripslashes() leaving multiple slashes