PHP 记录完整的请求和回复

需求如题,拼拼凑凑得到这个代码

<?php
ob_start();

echo "hello world";
setcookie("username", "123");

$output = ob_get_clean();
echo $output;
file_put_contents("log.txt", date("Y-m-d H:i:s") . "\n" . print_r(getallheaders(), true) . print_r($_REQUEST, true) . print_r(apache_response_headers(), true) . $output . "\n\n", FILE_APPEND);

我想要的,是记录下跟抓包工具中看到的一样的报文,然而,这个代码虽然尽了力,仍然没有达到要求,他记录的是类似

2014-12-13 19:27:28
Array
(
    [Host] => 10.185.12.103
    [Connection] => keep-alive
    [Cache-Control] => max-age=0
    [Accept] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    [User-Agent] => Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1612.2 Safari/537.36
    [Accept-Encoding] => gzip,deflate,sdch
    [Accept-Language] => zh-CN,zh;q=0.8                                                   
)
Array
(
    [name] => 123
)
Array
(
    [Set-Cookie] => username=123
)
hello world

要究其原因,其实还是因为 PHP 是被挡在 apache 后面,或者说被架在 Apache 之上,所以没有办法拿到完整报文

Leave a Reply

Your email address will not be published. Required fields are marked *