出于“安全原因”,我需要获取 PHP 的内容类型。例如,如果您使用了该函数header("Content-type: text/html; charset=utf-8")
,如何在将来执行时接收内容类型(text/html
) 和字符集 (utf-8
) 分别地?
我真正的问题是知道正在使用和修改哪个字符集only如有必要,无需重新定义信息Content-type
。即如果内容类型是application/xml
,保留它,但更改only字符集。
Ex.
Original: Content-type: text/html
First: -> set to application/xml.
Content-type: application/xml
Second: -> set charset.
Content-type: application/xml; charset=utf-8
Third: -> set to text/html
Content-type: text/html; charset=utf-8
怎么可能呢?
您可以使用该功能标题列表 http://www.php.net/manual/en/function.headers-list.php获取响应标头。从那里应该只是解析出相关的标头并在需要时重写。
$headers = headers_list();
// get the content type header
foreach($headers as $header){
if (substr(strtolower($header),0,13) == "content-type:"){
list($contentType, $charset) = explode(";", trim(substr($header, 14), 2));
if (strtolower(trim($charset)) != "charset=utf-8"){
header("Content-Type: ".trim($contentType)."; charset=utf-8");
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)