加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

从header()获取PHP内容类型

发布时间:2020-12-13 17:45:21 所属栏目:PHP教程 来源:网络整理
导读:我需要出于“安全原因”获取 PHP的内容类型.例如,如果您使用了函数头(“Content-type:text / html; charset = utf-8”),那么在将来的执行时,我将如何接收内容类型(text / html)和charset (utf-8)分开? 我真正的问题是知道正在使用哪个charset并在必要时仅
我需要出于“安全原因”获取 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");
              }
         }
  }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读