67

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 collects any POSTed data and emails it. I can foresee that the fields in the form are likely to change a lot over time, and so to save myself some time in the long run, I was wondering if I could write something that automatically gathered everything?

Is it possible?

Paul
  • 291
  • 6
  • 17
Chuck Le Butt
  • 47,570
  • 62
  • 203
  • 289
  • 2
    ofcourse just use the global arrays $_POST, $_GET, $_Ser..... – Khurram Ijaz Jun 13 '11 at 18:54
  • 3
    possible duplicate of [How to grab all variables in a post (PHP)](http://stackoverflow.com/questions/3058336/how-to-grab-all-variables-in-a-post-php) – mario Jun 13 '11 at 19:06

9 Answers9

127

Sure. Just walk through the $_POST array:

foreach ($_POST as $key => $value) {
    echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>";
}
Mathieu Turcotte
  • 344
  • 2
  • 14
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
49

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".

Roman Newaza
  • 11,405
  • 11
  • 58
  • 89
8

Yes you can use simply

     $input_data = $_POST;

or extract() may be useful for you.

xkeshav
  • 53,360
  • 44
  • 177
  • 245
  • 6
    `extract($_POST)` will mimic `register_globals`. Don't do that. – netcoder Jun 13 '11 at 19:02
  • 1
    @netcoder; i just suggest him that it can be useful; it is an option too. and will u please give me an example to make ur comments valuable. – xkeshav Jun 13 '11 at 19:13
  • 1
    Using `extract` on an unfiltered/unsanitized user supplied data is super, super bad idea. Do yourself a favor and don't bake insecurity into your code. – mickmackusa Jan 15 '19 at 23:46
5

All posted data will be in the $_POST superglobal.

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

George Cummins
  • 28,485
  • 8
  • 71
  • 90
4

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);
Tickthokk
  • 113
  • 1
  • 5
  • +1 The latter suggestion (`$body = print_r($_POST, true);`) worked for me as the simplest without having to iterate through child elements. I just needed to see the raw POST content temporarily while developing a webhook application. – SteveCinq Feb 05 '20 at 01:47
2

You can retrieve all the keys of the $_POST array using array_keys(), then construct an email messages with the values of those keys.

var_dump($_POST) will also dump information about all of the information in $_POST for you.

barfoon
  • 27,481
  • 26
  • 92
  • 138
1

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

Tarik
  • 79,711
  • 83
  • 236
  • 349
1

To add to the others, var_export might be handy too:

$email_text = var_export($_POST, true);
netcoder
  • 66,435
  • 19
  • 125
  • 142
searlea
  • 8,173
  • 4
  • 34
  • 37
0

Just to echo all data of $_POST.

echo json_encode($_POST);

You can also do this for more better viewing.

echo var_dump($_POST);