在PHP访问日志记录 [英] Access logging in PHP

查看:155
本文介绍了在PHP访问日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要登录访问任何文件,在 /文件文件夹,这样我就可以用PHP生成的一些统计数据进行处理。

I want to log access to any files in the /files folder, so I can process it with PHP to generate some statistics.

我不想写一个通过重写规则所谓定制的PHP处理程序,因为我不希望有处理状态codeS,MIME类型和缓存头,并且文件锁定的问题。

I don't want to write a custom PHP handler called via RewriteRule because I don't want to have to deal with status codes, MIME-types and caching headers, and file locking issues.

我没有访问服务器的配置,所以我不能使用的CustomLog (我有机会获得 .htacess )。

I don't have access to the server configuration, so I can't use CustomLog (I do have access to .htacess).

我不能使用 X-SENDFILE ,因为它未启用。

I can't use X-Sendfile because it's not enabled.

我没有访问的access.log

寻找一个权威的答案。

推荐答案

我已经尝试了许多伟大的事情,似乎有没有简单的解决办法。

I've tried a great many things and there seems to be no easy solution.

我的解决方案使用由@职场英语对话提出的位置头把戏,但我已经调整它符合我的preferences。

My solution uses the Location header trick proposed by @yes123 but I've tweaked it to match my preferences.

该文件的链接保持不变,所以它仍然是: /files/path/to/my/file.abc
我有一个重写规则

The links to the files are kept intact, so it's still: /files/path/to/my/file.abc I have a RewriteRule:

重写规则^文件/(.*)路径/要/ tracker.php?路径= / $ 1

然后通过添加?轨迹=没有来的网址和一个例外,我发出位置头文件中早前重写规则

Then in the file I issue a Location header by adding ?track=no to the URL and an exception to the earlier RewriteRule:

的RewriteCond%{QUERY_STRING}!(安培; | ^)=轨道无(安培; | $)

我增加了一个优化。我已经启用电子标签,所以如果客户端发送E-Tag头,,看它是否与文件匹配,返回的 304未修改代替位置

I've added one more optimization. I've enabled E-Tags so if the client send an E-Tag header, see if it matches the file and return a 304 Not Modified instead of a Location.

$fs = stat($document_root . $path);
$apache_etag = calculate_apache_etag($fs);
if ((isset($_SERVER["HTTP_IF_MATCH"]) && etag_within_range($_SERVER["HTTP_IF_MATCH"], $apache_etag))
    || (isset($_SERVER["HTTP_IF_NONE_MATCH"]) && etag_within_range($_SERVER["HTTP_IF_NONE_MATCH"], $apache_etag))
) {
    header("ETag: " . $apache_etag, true, 304);
    exit;
}

function etag_within_range($etag1, $etag2) {
    list($size1, $mtime1) = explode("-", $etag1);
    list($size2, $mtime2) = explode("-", $etag2);
    $mtime1 = floor(hexdec($mtime1) / 1000000);
    $mtime2 = floor(hexdec($mtime2) / 1000000);
    return $mtime1 === $mtime2 && $size1 === $size2;
}

和对 calculate_apache_etag 的实施可以在这里找到:的你怎么做匹配阿帕奇一个ETag?

And implementation for calculate_apache_etag can be found here: How do you make an etag that matches Apache?

etag_withing_range 解决了对更高的precision 的mtime 在Apache中比较的问题。

etag_withing_range solves the issue of comparing against a higher precision mtime in Apache.

解决方案上的注意事项,没有工作

虚拟

测试脚本:

var_dump(apache_response_headers());
virtual("/path/to/image.jpg");
var_dump(apache_response_headers());

输出:

array(1) { ["X-Powered-By"]=> string(10) "PHP/5.2.11" }
[[binary junk]]
array(5) { ["X-Powered-By"]=> string(10) "PHP/5.2.11" ["Keep-Alive"]=> string(18) "timeout=5, max=100" ["Connection"]=> string(10) "Keep-Alive" ["Transfer-Encoding"]=> string(7) "chunked" ["Content-Type"]=> string(9) "text/html" }

的Content-Type:text / html的 reaaaaalllly 的? (

也许PHP5.3的 header_remove 函数可以解决这个问题?我还没有尝试过。

Perhaps PHP5.3's header_remove function can solve this? I haven't tried.

这篇关于在PHP访问日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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