从header()获取PHP内容类型

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了从header()获取PHP内容类型脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要出于“安全原因获取 PHP内容类型.例如,如果您使用了函数头(“Content-tyPE:text / htML; charset = utf-8”),那么在将来的执行时,我将如何接收内容类型(text / html)和charset (utf-8)分开?

我真正的问题是知道正在使用哪个charset并在必要时仅修改它,而不必重新定义Content-type信息.即如果content-type是application / XMl,请保留它,但更改唯一的charset.

爆.

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

怎么可能?

解决方法

您可以使用函数 headers_list获取响应标头.从那里应该只是解析出相关的标题并在需要时重写.

$headers = headers_list();
  // get the content type header
  foreach($headers as $header){
         if (substr(strtolower($header),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");
              }
         }
  }

脚本宝典总结

以上是脚本宝典为你收集整理的从header()获取PHP内容类型全部内容,希望文章能够帮你解决从header()获取PHP内容类型所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。