Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Possible to automatically get all POSTed data?

Tags:

post

forms

php

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?

like image 566
Chuck Le Butt Avatar asked Jun 13 '11 18:06

Chuck Le Butt


People also ask

How can I get all POST variables in PHP?

Try var_dump($_POST); to see the contents. If your post data is in another format (e.g. JSON or XML, you can do something like this: $post = file_get_contents('php://input'); and $post will contain the raw data.

What is $_ POST [] in PHP?

PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post".

Can I use get and POST at the same time in PHP?

Show activity on this post. and then PHP will populate $_GET['foo'] as well, although the sent Request was POST'ed. However, your problem seems to be much more that you are trying to send two forms at once, directed at two different scripts. That is impossible within one Request.


2 Answers

Sure. Just walk through the $_POST array:

foreach ($_POST as $key => $value) {     echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>"; } 
like image 74
2 revs, 2 users 67% Avatar answered Sep 24 '22 11:09

2 revs, 2 users 67%


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

like image 21
Roman Newaza Avatar answered Sep 22 '22 11:09

Roman Newaza