dstbp22002 2012-03-07 11:18
浏览 87
已采纳

mysql / php - 将带有引号的后序列化数据插入db,并在以后检索它而不会产生斜线 - 安全问题

I am putting a serialized data to database. Data comes from input field and it can be anything (string), also with quotes signs. Serialized data I encode with base64 to preserve serialize to result error. But quotes marks became \'. I am aware that it is mysql injection remedy, and encoded data has crypted \' signs. Magic_quotes are on. Here's the code:

$_POST['post']:

[0] => this is \"test\"
[1] => test2
[2] => quotes \"\"

php:

$db  = base64_encode(serialize($_POST['post']));

Now i get serialized and encoded data. When I try to retrieve it:

[mysql functions to retrieve row]
$arr = unserialize(base64_decode($arr));
    foreach ( $arr as $arr1 ) {
    $arr2[] = stripslashes($arr1);
}
print_r($arr2);

After data retrieval I get as intended:

$arr2:

[0] => this is "test"
[1] => test2
[2] => quotes ""

Question is: is it mysql injection safe? Is it safe at all to put data from arr2 into html page into input fields and body of page? If not what would be a good method to do it? Should I use html_escape and mysql_special like functions ?

Thanks in advance :)

EDIT: string data from database will be used only as a value of input and textarea html tags.

  • 写回答

2条回答 默认 最新

  • douyi3676 2012-03-07 14:38
    关注

    It's happening because you've got magic quotes enabled. Magic quotes are deprecated.

    Since you're using your own method to avoid injection issues, if you are unable to change the magic quotes setting for your installation, then stripslashes before you base64_encode the data - not when you decode it.

    (I assume you are aware that the data structure you are creating is opaque to the DBMS thereby breaking normalization rules and making it impossible to query the data effectively).

    string data from database will be used only as a value of input and textarea html tags.

    So you may (at a significant cost) have solved the sql injection problem but are not bothered about XSS attacks?

    Use the provided functions for changing the representation of data according to where you are sending the data (i.e. mysql_real_escape_string, htmlentities etc).

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

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本