dton37910 2012-07-19 05:26
浏览 109
已采纳

php $ POST动态变量命名安全问题

I am thinking of making a loop to gather all my $_POST variables and assign them to dynamically named variables.Something like this (not tested)

 for($i; $i <= $_POST[].length; $i++){
  ${$_POST[i]} = $_POST[i]
  }

But I am wondering about the security of something like this. This would then create a variable in the system for every bit of post data sent to the page. Can that variable be damaging even if the script I write doesn't reference it? Is this the type of thing I should avoid entirely? I have some pages that send quite a few variables and a script like this would prevent a whole lot of writing, but is it safe enough?

  • 写回答

7条回答 默认 最新

  • dtpd58676 2012-07-19 05:50
    关注

    Yes there can be security concerns/problems, for example one could overwrite any local variables which are already set, like database, config values ect.

    So something like this should be avoided:

    $yourImportantVar = 'Something relies on this';
    
    //User POSTS yourImportantVar=overwritten
    foreach ($_POST as $key => $value) {
        $$key = $value; 
    }
    echo $yourImportantVar; //overwritten 
    

    But if you want to implement a loop to save a chunk of code, you could create an allowed array which you loop over and extract out the value from the $_POST.

    foreach (array(
        'name',
        'address',
        'somethingelse',
        'ect'
    ) as $key) {
        $$key = isset($_POST[$key]) ? $_POST[$key] : null;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?