Log GET requests and querystring in PHP (Apache)

。_饼干妹妹 提交于 2019-12-13 04:18:52

问题


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

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".

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


回答1:


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

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

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 requests can then be found in /private/var/log/apache2/{hostname}-error.log



来源:https://stackoverflow.com/questions/54008051/log-get-requests-and-querystring-in-php-apache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!