在 PHP (Apache) 中记录 GET 请求和查询字符串 [英] Log GET requests and querystring in PHP (Apache)

查看:63
本文介绍了在 PHP (Apache) 中记录 GET 请求和查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个在 Apache 上运行的 PHP 应用程序,并且想要记录所有 API 请求(GET + 参数).

We have a PHP application running on Apache and want to log all API requests (GET + parameters).

我看过这篇文章 在 Apache 中记录 POST 数据的最佳方法? 哪里说GET 请求将很容易,因为它们将在 apache 日志中".

I have seen this post Best way to log POST data in Apache? where it says "the GET requests will be easy, because they will be in the apache log".

然而,当我查看我们的日志时,它们并不存在.我需要哪些服务器日志设置来记录 GET 请求 + 查询字符串?https://httpd.apache.org/docs/中没有提及如何执行此操作2.4/logs.html

However, when I look in our logs, they are not there. What server log settings do I need to have to record GET requests + querystring? No mention of how to do this in https://httpd.apache.org/docs/2.4/logs.html

推荐答案

GET 请求记录在访问日志文件中.阅读您提供的文档,尤其是 Access Log 部分很重要.您的 Apache 主机应配置如下:

The GET request are logged in the access log file. Read the documentation you provided, especially the Access Log part is important. Your Apache host should be configured with something like:

LogLevel        info
ErrorLog        "/private/var/log/apache2/{hostname}-error.log"
CustomLog       "/private/var/log/apache2/{hostname}-access.log" combined

然后可以在 /private/var/log/apache2/{hostname}-access.log

出于调试目的,一种简单快捷的方法是编写一个记录 POST 数据的函数.

An easy and quick way to do this for debugging purposes is to write a function that logs the POST data.

function logPost() {
    if (isset($_POST && !empty($_POST) {
        foreach($_POST as $key => $value) {
            error_log('=== _POST REQUEST ===');
            error_log('_POST: '.$key.' => '.$value);
        }
        // OR serialise the data but this is less readable
        error_log('=== _POST REQUEST ===');
        error_log(serialise($_POST));
    }
}

POST 请求可以在 /private/var/log/apache2/{hostname}-error.log

POST requests can then be found in /private/var/log/apache2/{hostname}-error.log

这篇关于在 PHP (Apache) 中记录 GET 请求和查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆