새소식

Php

[PHP] header 란 ?

  • -
728x90

PHP Header 란? 

오늘은 가공한 데이터를 엑셀로 다운받는 과정에서 php 의 header() 함수를 사용하는데 정확한 의도와 사용법에 대해 숙지하기 위해 알아본 내용들을 정리하고자 한다.

 

https://www.php.net/manual/en/function.header.php

 

PHP: header - Manual

After lots of research and testing, I'd like to share my findings about my problems with Internet Explorer and file downloads.  Take a look at this code, which replicates the normal download of a Javascript: Now let me explain:  I start out by checking f

www.php.net

header — Send a raw HTTP header

공식 문서에 나와있는 내용이다. 즉, 헤더 메세지는 서버와 클라이언트간 본격적으로 송수신하기 전에 있어서 필요한 정보들을 사전에 나누는 것이라고 생각하면 된다.

 

PHP 스크립트는 종종 클라이언트 브라우저 또는 서버와 클라이언트 브라우저 간의 프록시 캐시에 의해 캐시되지 않아야 하는 동적 콘텐츠를 생성한다. 예를 들면 파일명이 같지만 다른 데이터를 다운로드 받는 경우에 내용이 수정되어도 캐싱된 데이터로 호출받는 경우가 생길 수도 있다.

많은 프록시와 클라이언트는 header의 일부 기능을 통해 캐싱을 강제로 비활성화 할 수 있다.

 

필자는 header 함수를 이용하여 가공한 데이터를 엑셀로 다운받는데 캐시를 비활성화 하기 위해 사용한 코드를 중점으로 정리했다.

 

header, 캐시 비활성화

header( "Content-type: application/vnd.ms-excel; charset=EUC-KR" );   
header( "Expires: 0" );                                               
header( "Cache-Control: must-revalidate, post-check=0,pre-check=0" ); 
header( "Pragma: public" );                                           
header( "Content-Disposition: attachment; filename=excel.csv" );

header( "Content-type: application/vnd.ms-excel; charset=EUC-KR" );   

: Content-type 을 위와 같이 지정해 줌으로써 xls로 출력한다는 것을 의미하고 , charset 을 EUC-KR 로 변경

 

header( "Expires: 0" );                                             

: [캐시방지] 유효기간을 의미하는데, 0 보다는확실히 과거인 날짜로 사용하는 것을 권장한다고 한다.

 

header( "Cache-Control: must-revalidate, post-check=0,pre-check=0" ); 

: [캐시방지] HTTP/1.1

 

header( "Pragma: public" );                                           

: [캐시방지] HTTP/1.0, IE와 같은 구형 브라우저를 위한 보험 ?

 

header( "Content-Disposition: attachment; filename=header.csv" );   

: 대화 상자를 표시하고, 파일명을 header.csv 로 지정

 

엑셀 파일에 대한 Content-type

추가로 조사해보니 아래와 같은 Content-type 이 추가로 있는데 2개만 정리!

 

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
: xlsx 형식

application/vnd.ms-excel
: xls 형식

 

참고자료

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control

 

Cache-Control - HTTP | MDN

The Cache-Control HTTP header field holds directives (instructions) — in both requests and responses — that control caching in browsers and shared caches (e.g. Proxies, CDNs).

developer.mozilla.org

 

 

급하게 조사하고 정리하다보니 부족하거나 틀린 내용이 있을 수도 있는데 댓글로 달아주시면 감사하겠습니다.

728x90

'Php' 카테고리의 다른 글

[PHP] base64_encode 란  (0) 2023.07.24
[Laravel] Cookie's domain  (0) 2023.01.03
[PHP] 자주쓰는 메서드 정리  (0) 2022.06.21
[PHP] crontab  (0) 2022.06.20
[Laravel] DB 수정 내역 배포  (0) 2022.01.06
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.