duanjiebian6712 2014-12-14 18:29
浏览 37

如何访问发布到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 )

  • 写回答

2条回答 默认 最新

  • doushi2047 2014-12-14 18:33
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大