doujie4344 2014-10-19 22:49
浏览 56
已采纳

php加倍\字符(打破mysql_real_escape_string,PDO->引用等)[重复]

This question already has an answer here:

So PHP is having a lot of trouble dealing with ' characters in strings recently in one of my projects, and I think the main reason behind this is for some crazy reason it's doubling the \ character. I've checked, and magic quotes are off (so this is not the culprit). Anyways, given the following code:

26 $comments = $_POST['comments'];
27 error_log("comments: '$comments'");
28 $comments = mysql_real_escape_string($_POST['comments']);
29 error_log("escaped comments: '$comments'");

I'm seeing the following in the error log:

[Sun Oct 19 14:18:53 2014] [error] [client XXXX] comments: 'something elsewearwerawer's woeimrowiamrw', referer: ...
[Sun Oct 19 14:18:53 2014] [error] [client XXXX] comments escaped: 'something elsewearwerawer\\'s woeimrowiamrw', referer: ...

Even worse, I still see the same behavior after swapping things over to PDO:

error_log("quoted: '" . $db_pdo->quote($comments) . "'");

Even when I do something simple like:

error_log('\\');

or

error_log("\\");

The error log shows:

[Sun Oct 19 17:44:57 2014] [error] [client XXXX] \\, referer: ...

Any idea what is going on here? I'm worried because it looks like this means mysql_real_escape_string (or PDO) is not correctly escaping single quotes in strings, which could lead to a SQL injection. Whenever I try and update/insert with a string with a ' in it, even after calling mysql_real_escape_string or by using quote (or bindParam with a string), it doesn't insert anything after the '

SOLVED: After digging deeper it was actually inserting things into the database correctly, the error was happening on the other end of things when the webpage was pulling from the database and not dealing with the ' correctly, so it was getting cut off in the html.

</div>
  • 写回答

2条回答 默认 最新

  • dqfxao2898 2014-10-19 23:01
    关注

    You need to turn off magic_quotes_gpc parameter in your php.ini config.

    http://php.net/manual/en/security.magicquotes.disabling.php

    As a workaround you can remove the slashes it's adding automatically, using stripslashes(), by doing this:

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

    or this (using PDO)

    $comments = $db_pdo->quote( stripslashes( $comments ) );

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题