我正在运行一个 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/