2013-07-15 3 views
4

"보안상의 이유로"PHP의 콘텐츠 유형을 얻어야합니다. 예를 들어, header("Content-type: text/html; charset=utf-8") 함수를 사용한 경우 향후 실행 시간에 콘텐츠 유형 (text/html)과 문자 집합 (utf-8)을 각각받을 수 있습니까?header()에서 PHP 콘텐츠 유형 가져 오기

내 진짜 문제는 사용하고 수정되고있는 캐릭터 세트 알고있다 이, 정보 Content-type를 다시 정의 할 필요없이, 필요한 경우. 즉 콘텐츠 유형이 application/xml 인 경우 그대로 유지하지만 문자 집합 만 변경하십시오.

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 

어떻게 가능합니까?

+0

당신은'Accept' 헤더를 붙잡기를 원하십니까 – DevZer0

답변

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"); 
       } 
     } 
    } 
0

또한

http://php.net/manual/en/function.get-headers.php

그냥 당신이 콘텐츠 유형을 "수정 만"할 수 없습니다 명심() 함수 get_headers을 사용할 수 있습니다. 그것을 변경하려면 header() 함수를 다시 호출해야합니다.

+1

get_headers get은 원시 요청 헤더입니다 .... 저는 op가 응답 헤더 뒤에 있다고 믿습니다. – Orangepill