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

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路