在 PHP 中获取当前的 Content-Type 标头

Rap*_*pti 4 php logging http-headers

我正在运行一个 PHP 脚本来将所有对我的站点的请求记录在 MySQL 数据库中。例如,我网站上的一些脚本还修改Content-Type用于输出图像的标题。header("Content-Type: image/png");我也在尝试记录这个内容类型。如何获取包含 Content-Type 的字符串变量?

And*_*erj 5

你的脚本是什么样的?什么时候执行?如果它在每个脚本的末尾执行,您可以使用php 函数 headers-list检查当前设置的标头

这是一个如何使用它的简单示例:

<?php
// headers sent
$headers = headers_list(); // get list of headers
foreach ($headers as $header) { // iterate over that list of headers
  if(stripos($header,'Content-Type') !== FALSE) { // if the current header hasthe String "Content-Type" in it
    $headerParts = explode(':',$header); // split the string, getting an array
    $headerValue = trim($headerParts[1]); // take second part as value
    $yourLoggingService->setContentTypeOfCurrentCall($headerValue);
  }
}
Run Code Online (Sandbox Code Playgroud)