如何访问发布到PHP服务器的所有值

I first thought it was in the $_POST super global, but it isn't if values are included in the URL.

$_REQUEST did so and surprised me by not including cookies (reference http://php.net/manual/en/reserved.variables.request.php), and I later found that I am evidently using the default distribution php.ini file which does not contain the 'C' for cookies (reference http://php.net/manual/en/ini.core.php#ini.request-order). I don't wish to use $_REQUEST, however, as it doesn't differentiate between a get request, and changing servers and php.ini files could cause a security concern.

What is the proper way to access all post values?

EDIT. I added the $real_post part. Is this the proper way to do so?

<?php
setcookie('cookie', 'COOKIE', time() + (86400 * 30), "/");

echo('$_GET<pre>'.print_r($_GET,1).'</pre>');
echo('$_POST<pre>'.print_r($_POST,1).'</pre>');
echo('$_COOKIE<pre>'.print_r($_COOKIE,1).'</pre>');
echo('$_REQUEST<pre>'.print_r($_REQUEST,1).'</pre>');
$real_post=($_SERVER['REQUEST_METHOD'] == 'POST')?array_merge($_GET,$_POST):array(); 
echo('$real_post<pre>'.print_r($real_post,1).'</pre>');
?>

<form action='postorget.php?get=GET' method='post'>
    <input type='text' name='post' value='POST'>
    <input type='submit'>
</form>

$_GET

Array ( [get] => GET )

$_POST

Array ( [post] => POST )

$_COOKIE

Array ( [cookie] => COOKIE )

$_REQUEST

Array ( [get] => GET [post] => POST )

Sorry, your question is a little unclear. Parameters appended to the URL (as you mention in the beginning) are GET parameters, so they are contained in the $_GET superglobal. They simply are not POST variables. So what is your question here? You could combine $_POST and $_GET or, preferred, check for a desired parameter in both locations.

You can invest endless time into stuff like this, but a convenient approach might look like this:

$param = isset($_GET['param']) ? $_GET['param'] 
                               : (isset($_POST['param']) ? $_POST['param'] 
                                                         : null);

This line is just an example. It retrieves a parameter called 'param' from the super globals $_GET or $_POST and stores it in the variable $params in the local scope. This way you can access any parameter you are looking for regardless if it is sent as a GET or as a POST parameter. I often wrap that in a convenience function which also takes care of validating the parameters runtime value.

You could also wrap this example in an iteration loop:

$params = [
    'id'     => null, 
    'key'    => null, 
    'value'  => null, 
    'remark' => null
]; // just as examples
foreach ($params as $key=>$null) {
    // alternative 1: store the value of param $key in a single local scalar variable
    // this results in local variables $id, $key, $value, $remark, just as examples
    $$key = isset($_GET[$key]) ? $_GET[$key] 
                               : (isset($_POST[$key]) ? $_POST[$key] 
                                                      : null);
    // alternative 2: store the value of param $key in a general but local params array
    // this results in the above $params array, but filled with scalar values
    $params[$key] = isset($_GET[$key]) ? $_GET[$key] 
                                       : (isset($_POST[$key]) ? $_POST[$key] 
                                                              : null);
}

These are actually two examples. Obviously you need only one of the two statements shown inside the loop here. This depends on whether you prefer an array of params or separate local scalar variables.


If you are looking for a way to get all GET and POST parameters without actually knowing which that might be, then you have to combine the two super globals:

$params = array_merge($_GET, $_POST);

Note however that this is a very questionable architecture. It typically opens security holes.

You could do something like:

$uVariables = array("GET" => $_GET, "POST" => $_POST, "COOKIES" => $_COOKIES, "SESSION" => $_SESSION);

and then use json_encode() for database storage. Should you later decide to build a log viewer you can just use json_decode() and get everything back in original state.