[php] Get all variables sent with POST?

I need to insert all variables sent with post, they were checkboxes each representing an user.

If I use GET I get something like this:

?19=on&25=on&30=on

I need to insert the variables in the database.

How do I get all variables sent with POST? As an array or values separated with comas or something?

This question is related to php http-post

The answer is


Why not this, it's easy:

extract($_POST);

It is deprecated and not wished to access superglobals directly (since php 5.5 i think?)

Every modern IDE will tell you:

Do not Access Superglobals directly. Use some filter functions (e.g. filter_input)

For our solution, to get all request parameter, we have to use the method filter_input_array

To get all params from a input method use this:

$myGetArgs = filter_input_array(INPUT_GET);
$myPostArgs = filter_input_array(INPUT_POST);
$myServerArgs = filter_input_array(INPUT_SERVER);
$myCookieArgs = filter_input_array(INPUT_COOKIE);
...

Now you can use it in var_dump or your foreach-Loops

What not works is to access the $_REQUEST Superglobal with this method. It Allways returns NULL and that is correct.

If you need to get all Input params, comming over different methods, just merge them like in the following method:

function askForPostAndGetParams(){
    return array_merge ( 
        filter_input_array(INPUT_POST), 
        filter_input_array(INPUT_GET) 
    );
}

Edit: extended Version of this method (works also when one of the request methods are not set):

function askForRequestedArguments(){
    $getArray = ($tmp = filter_input_array(INPUT_GET)) ? $tmp : Array();
    $postArray = ($tmp = filter_input_array(INPUT_POST)) ? $tmp : Array();
    $allRequests = array_merge($getArray, $postArray);
    return $allRequests;
}

So, something like the $_POST array?

You can use http_build_query($_POST) to get them in a var=xxx&var2=yyy string again. Or just print_r($_POST) to see what's there.


Using this u can get all post variable

print_r($_POST)

you should be able to access them from $_POST variable:

foreach ($_POST as $param_name => $param_val) {
    echo "Param: $param_name; Value: $param_val<br />\n";
}