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

查看:163
本文介绍了在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

GET请求

一种用于调试目的的简便方法是编写一个记录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));
    }
}

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

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

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