4

「セキュリティ上の理由」から、PHP の content-type を取得する必要があります。たとえば、関数 を使用した場合header("Content-type: text/html; charset=utf-8")、将来の実行時に content-type ( text/html) と charset ( utf-8) を別々に受け取るにはどうすればよいですか?

私の本当の問題は、どの文字セットが使用されているかを知り、必要に応じて情報を再定義することなく、それのみContent-typeを変更することです。つまり、 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

それはどのように可能ですか?

4

2 に答える 2

7

関数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");
              }
         }
  }
于 2013-07-15T03:27:56.947 に答える