PHP: Possible to automatically get all POSTed data?

后端 未结 8 1582
Happy的楠姐
Happy的楠姐 2020-11-28 05:20

Simple question: Is it possible to get all the data POSTed to a page, even if you don\'t know all the fields?

For example, I want to write a simple script that colle

相关标签:
8条回答
  • 2020-11-28 05:50

    No one mentioned Raw Post Data, but it's good to know, if posted data has no key, but only value, use Raw Post Data:

    $postdata = file_get_contents("php://input");
    

    PHP Man:

    php://input is a read-only stream that allows you to read raw data from the request body. In the case of POST requests, it is preferable to use php://input instead of $HTTP_RAW_POST_DATA as it does not depend on special php.ini directives. Moreover, for those cases where $HTTP_RAW_POST_DATA is not populated by default, it is a potentially less memory intensive alternative to activating always_populate_raw_post_data. php://input is not available with enctype="multipart/form-data".

    0 讨论(0)
  • 2020-11-28 05:52

    As long as you don't want any special formatting: yes.

    foreach ($_POST as $key => $value) 
        $body .= $key . ' -> ' . $value . '<br>';
    

    Obviously, more formatting would be necessary, however that's the "easy" way. Unless I misunderstood the question.

    You could also do something like this (and if you like the format, it's certainly easier):

    $body = print_r($_POST, true);
    
    0 讨论(0)
  • 2020-11-28 05:55

    All posted data will be in the $_POST superglobal.

    http://php.net/manual/reserved.variables.post.php

    0 讨论(0)
  • 2020-11-28 06:03

    You can use $_REQUEST as well as $_POST to reach everything such as Post, Get and Cookie data.

    0 讨论(0)
  • 2020-11-28 06:10

    Sure. Just walk through the $_POST array:

    foreach ($_POST as $key => $value) {
        echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>";
    }
    
    0 讨论(0)
  • 2020-11-28 06:12

    Yes you can use simply

         $input_data = $_POST;
    

    or extract() may be useful for you.

    0 讨论(0)
提交回复
热议问题