简体   繁体   中英

php log complete request

Is it possible to log the request with php? I also want to log the images, js, css file requests.

<img src="foo.png">
<link rel="stylesheet" href="foo.css">

I currently use this code but it only gives me the current request uri and not the other files etc. I also rewrited all request to my index.php wich i have this line of code in.

file_put_contents('foo.txt', $_SERVER['REQUEST_URI'] . PHP_EOL, FILE_APPEND | LOCK_EX);

Here is one option to draw all http requests to a file. This applies to all requests that travel with the HTTP protocol

$myFile = "requestslog.txt";
    $fh = fopen($myFile, 'a') or die("can't open file");
    fwrite($fh, "\n\n--------------------------------------
        -------------------------\n");
    foreach($_SERVER as $h=>$v)
        if(ereg('HTTP_(.+)',$h,$hp))
            fwrite($fh, "$h = $v\n");
    fwrite($fh, "\r\n");
    fwrite($fh, file_get_contents('php://input'));
    fclose($fh);
    echo "<html><head /><body><iframe src=\"$myFile\" 
        style=\"height:100%; width:100%;\"></iframe></body></html>"

Yes, it is. You can make all the requests pass through a php script, so that you can log the action. For example, a simple image request like http://url.com/img.jpg would became http://url.com/index.php?action=download&file=img.jpg , and the script would handle the logging, the file download and correct headers.

Also take into account that your http server might be logging the request already, take a look into the access_log of apache if you are using it.

I prefer the approach listed on this gist .

<?php
// 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');

You can quickly copy the above file by doing curl -O https://gist.githubusercontent.com/magnetikonline/650e30e485c0f91f2f40/raw/cbc114d0af29eaad80f75b69732d757971c71fd0/dumprequest.php > dumprequest.php .

The output will be something like

GET /dumprequest.php HTTP/1.1

HTTP headers: Authorization: User-Agent: PostmanRuntime/7.29.0 Accept: / Cache-Control: no-cache Host: somehost.com Accept-Encoding: gzip, deflate, br Connection: keep-alive

Request body: hi=ricardo

// Reports all errors
error_reporting(E_ALL);
// Do not display errors for the end-users (security issue)
ini_set('display_errors','Off');
// Set a logging file
ini_set('error_log','request_log_file.log');

Note- request_log_file.log - you can set your full file path here if needed.

Hope this will help you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM