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条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?