php - 在 PHP 中获取当前的 Content-Type header

标签 php logging http-headers

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

最佳答案

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

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

<?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);
  }
}

关于php - 在 PHP 中获取当前的 Content-Type header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22478703/

相关文章:

php - 在 Excel 中查看多个 HTML 表格时无法设置 TD 宽度

php - 有什么方法可以使用 PHP 或 Xslt 提取单个 XML 标记的各个部分?

php - 如何自动循环每个组(类别)

php - 如何添加到 MySQL 数据库中的当前值

python - 机器人框架和 Python 日志记录 : Logging from multiple threads and logging channel names

logging - 使用 log4j 进行 Keycloak 日志记录以使用自定义滚动附加程序

php - Mysql INSERT 查询插入零

json - 在 Node.js/Express 中记录所有请求

css - Content-Language 的响应头支持

javascript - 将 Firebase 身份验证 token 发送到 Nodejs 服务器