Dav*_*ues 4 php content-type header http
我需要出于"安全原因"获取PHP的内容类型.例如,如果你使用了这个函数header("Content-type: text/html; charset=utf-8"),在将来的执行时,我如何分别收到content-type(text/html)和charset(utf-8)?
我真正的问题是知道正在使用哪个字符集,并在必要时仅修改它,而无需重新定义信息Content-type.即如果内容类型是application/xml,请保留它,但更改唯一的字符集.
防爆.
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
Run Code Online (Sandbox Code Playgroud)
怎么可能?
您可以使用函数headers_list来获取响应标头.从那里应该只是解析出相关的标题并在需要时重写.
$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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10036 次 |
| 最近记录: |