使用PHP并且没有HTTP身份验证将用户名放在apache access_log中

chi*_*org 12 php apache authentication logging http

在Apache日志配置中,可以指定应记录HTTP身份验证用户名.大多数PHP脚本都有自己的基于cookie的身份验证.是否有可能在PHP中为Apache提供HTTP auth用户名以进行日志记录,即使身份验证是基于cookie的?如果是,代码将如何?如果没有,有什么替代方案?

Jan*_*bry 13

Apache在注释中的模块之间传递数据.如果您将PHP作为Apache模块运行,则可以使用它apache_note()来获取和设置注释.然后,您可以%{note_name}n日志格式字符串写这篇访问日志.这不会将任何数据"泄漏"回客户端.

在PHP中:

apache_note( 'username', $username );
Run Code Online (Sandbox Code Playgroud)

在您的服务器配置中:

LogFormat "%h %l %{username}n %t \"%r\" %>s %b" common_with_php_username
CustomLog logs/access_log common_with_php_username
Run Code Online (Sandbox Code Playgroud)

  • 如果未设置,则使用“-”。 (3认同)

tow*_*owr 9

由于Apache 2.4.7 Apache允许您将响应标头复制到注释中.所以,如果你不运行PHP作为Apache模块(但例如使用PHP-FPM),你也不想对数的值被发送到客户端(如果在设置这通常会发生response-header),这是一种方法:

PHP:

header('X-Username: '.$username);
Run Code Online (Sandbox Code Playgroud)

httpd.conf中:

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{username}n\"" logfmtname
Run Code Online (Sandbox Code Playgroud)

vhost.conf:

CustomLog logs/vhost-access_log logfmtname

# copy response-header value to note
Header note X-Username username
# unset response-header so client won't get it
Header unset X-Username
Run Code Online (Sandbox Code Playgroud)