douxiongzhen2126 2013-03-06 02:38
浏览 49
已采纳

PHP:extract($ _ POST)然后get_defined_vars()什么都不返回...为什么?

I have the following PHP script:

<?php 
  function dump_extracted_post() {
    extract($_POST);
    var_dump(get_defined_vars());
  }
  dump_extracted_post();
?>

To this script, I post a form containing some data, and it produces the following (unexpected) result:

Array(0) {
}

Why do I not see the the posted data?

Is there something I've missed in my understanding of extract() or get_defined_vars()? Is there some sort of security setting that might be interfering with extracting from $_POST ?

I am aware of the frowned upon nature of extract, so perhaps it's a security issue, but scanning the php docs doesn't seem to mention any such issue. I want to use this to do some simple templating.

If I change the script to:

<?php 
  function dump_extracted_post() {
    var_dump($_POST);
  }
  dump_extracted_post();
?>

I see the posted data as expected (so yes, I am definately posting data).

Likewise, if I change the script to:

<?php 
  function dump_extracted_post() {
    $a = 'moocows';
    var_dump(get_defined_vars());
  }
  dump_extracted_post();
?>

I see the expected output:

array(1) {
  ["a"]=>
  string(7) "moocows"
}
  • 写回答

2条回答 默认 最新

  • dongzhaoshi8497 2013-03-06 06:32
    关注

    Your original function works fine for me. I used the following script (named extract.php) for testing:

    <form method="post" aciton="extract.php">
      <input type="text" name="foo" />
      <input type="submit" />
    </form>
    <pre>
    <?php
      function dump_extracted_post() {
        extract($_POST);
        var_dump(get_defined_vars());
      }
      dump_extracted_post();
    ?>
    </pre>
    

    Function extract extracts only those key=>value pairs where key is valid identifier not conflicting with existing variables. So probably keys in your $_POST are either not a valid identifiers or are conficting with existing variables.

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器