【问题标题】:PHP dump $_REQUEST to filePHP 转储 $_REQUEST 到文件
【发布时间】:2011-03-20 16:22:52
【问题描述】:

我想将请求变量转储到文件中以进行调试。这怎么可能?

【问题讨论】:

    标签: php


    【解决方案1】:
    <?php
    $req_dump = print_r($_REQUEST, TRUE);
    $fp = fopen('request.log', 'a');
    fwrite($fp, $req_dump);
    fclose($fp);
    

    未经测试但应该可以完成这项工作,只需将 request.log 更改为您要写入的文件即可。

    【讨论】:

    • 微小的无关紧要的细节,但忘记结束了吗?>
    • Closing ?> 完全没有必要。实际上,最好的做法是在编写库/等(与此处无关)时省略它,以确保不会意外输出可能会弄乱输出缓冲/标题/等的空白。
    • 我个人喜欢var_export($var,true)
    • @josh A close ?> 没有必要,实际上它不是一个好习惯。您可以在结束标记后获得额外的空格,这可能会导致脚本出错
    • 我改用fwrite($fp, '['.date("c").']'.$req_dump."\n");。这样,当我tail -f该文件时,我得到一个日期/时间戳。
    【解决方案2】:

    我认为现在这种方法更简单快捷:

    $req_dump = print_r($_REQUEST, true);
    $fp = file_put_contents('request.log', $req_dump, FILE_APPEND);
    

    【讨论】:

    • 您可能希望附加到日志:file_put_contents( 'request.log', $req_dump, FILE_APPEND)
    • 这里为什么需要$fp 变量?
    • 您不需要它,但它会包含truefalse 值,具体取决于操作是否成功。
    【解决方案3】:

    使用serialize() 函数进行转储。分别转储$_SERVER$_COOKIE$_POST$_GET(可能转到同一个文件)。如果您计划使用数据进行调试,了解数据是 POST 请求还是 GET 请求的一部分会有所帮助。

    转储所有内容有利于在开发中进行调试,但在生产中则不然。如果您的应用程序没有很多用户,它也可以在生产环境中工作。如果您预计会有很多用户,请考虑仅转储 $_POST 数据,或将服务器变量限制为以 HTTP_ 开头的变量。

    【讨论】:

      【解决方案4】:
      /* may be late but he can help others.
      it's not my code, I get it from : 
      https://gist.github.com/magnetikonline/650e30e485c0f91f2f40
      */
      
                  class DumpHTTPRequestToFile {
                      public function execute($targetFile) {
                          $data = sprintf(
                              "%s %s %s\n\nHTTP headers:\n",
                              $_SERVER['REQUEST_METHOD'],
                              $_SERVER['REQUEST_URI'],
                              $_SERVER['SERVER_PROTOCOL']
                          );
                          foreach ($this->getHeaderList() as $name => $value) {
                              $data .= $name . ': ' . $value . "\n";
                          }
                          $data .= "\nRequest body:\n";
                          file_put_contents(
                              $targetFile,
                              $data . file_get_contents('php://input') . "\n"
                          );
                          echo("Done!\n\n");
                      }
                      private function getHeaderList() {
                          $headerList = [];
                          foreach ($_SERVER as $name => $value) {
                              if (preg_match('/^HTTP_/',$name)) {
                                  // convert HTTP_HEADER_NAME to Header-Name
                                  $name = strtr(substr($name,5),'_',' ');
                                  $name = ucwords(strtolower($name));
                                  $name = strtr($name,' ','-');
                                  // add to list
                                  $headerList[$name] = $value;
                              }
                          }
                          return $headerList;
                      }
                  }
                  (new DumpHTTPRequestToFile)->execute('./dumprequest.txt');
      
                  // add this line at the end to create a file for each request with timestamp
      
                  $date = new DateTime();
                  rename("dumprequest.txt", "dumprequest" . $date->format('Y-m-d H:i:sP') . ".txt");
      

      【讨论】:

        猜你喜欢
        • 2012-12-22
        • 2013-06-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多