代码之家  ›  专栏  ›  技术社区  ›  chiborg Alessandro Minoccheri

将用户名放入带有PHP和不带HTTP验证的apache访问日志

  •  9
  • chiborg Alessandro Minoccheri  · 技术社区  · 14 年前

    4 回复  |  直到 14 年前
        1
  •  17
  •   Jan Fabry    13 年前

    Apache在中的模块之间传递数据 . 如果将PHP作为Apache模块运行,则可以使用 apache_note() 记笔记。然后可以包括 the %{note_name}n log format string

    在PHP中:

    apache_note( 'username', $username );
    

    在服务器配置中:

    LogFormat "%h %l %{username}n %t \"%r\" %>s %b" common_with_php_username
    CustomLog logs/access_log common_with_php_username
    
        2
  •  10
  •   towr    10 年前

    因为apache2.4.7apache允许您将响应头复制到注释。因此,如果您不将PHP作为Apache模块运行(例如,使用PHP-FPM),并且您也不希望将日志值发送到客户端(如果在响应头中设置日志值,通常会发生这种情况),那么可以使用以下方法:

    菲律宾比索:

    header('X-Username: '.$username);
    

    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{username}n\"" logfmtname
    

    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
    
        3
  •  2
  •   Wrikken    14 年前

    一种可能性是存储用户名;过去的session\u id在其他地方,并让日志在其中写入cookie值(通常是 %{PHPSESSID}C

    另一种选择是将带有用户名的头发送回客户端,最好是在您的 session_start

    header('X-Php-Sess-User: '.$username);
    

    自定义日志:

    %{X-Php-Sess-User}o
    
        4
  •  0
  •   codehead    14 年前

    除了使用Apache处理程序来处理内部auth*数据结构之外,最好的办法是使用环境变量。您可以在PHP代码中使用apache\u setenv设置顶级环境变量

    apache_setenv('USERID','jrodriguez',true);
    

    LogFormat "%v:%p %h %l %{USERID}e %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" envuid_combined
    CustomLog /path/to/access.log envuid_combined
    

    当然,真正的凭证时,执行 HTTP auth将永远丢失,因此请考虑将%u保存到其他位置--在新字段或并行日志文件中。