Get all variables sent with POST?

后端 未结 6 1967
孤城傲影
孤城傲影 2020-11-27 02:45

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=         


        
6条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-27 03:16

    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;
    }
    

提交回复
热议问题