PHP 헤더() 함수
예시
페이지 캐싱을 방지하기 위해 세 개의 HTTP 헤더를 보냅니다.
<?php
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
?>
<html>
<body>
...
...
참고: 사용자가 브라우저의 기본 캐싱 설정을 변경하도록 설정할 수 있는 옵션이 있습니다. 위의 헤더를 보내면 해당 설정을 무시하고 브라우저가 캐시하지 않도록 합니다!
정의 및 사용
header() 함수는 원시 HTTP 헤더를 클라이언트에 보냅니다.
실제 출력이 전송되기 전에 header() 함수가 호출되어야 한다는 점에 유의하는 것이 중요합니다!
통사론
header(header, replace, http_response_code)
매개변수 값
Parameter | Description |
---|---|
header | Required. Specifies the header string to send |
replace | Optional. Indicates whether the header should replace a previous similar header or add a new header of the same type. Default is TRUE (will replace). FALSE allows multiple headers of the same type |
http_response_code | Optional. Forces the HTTP response code to the specified value |
기술적 세부 사항
반환 값: | 아무것도 |
---|---|
PHP 버전: | 4.0+ |
PHP 변경 로그: | PHP 5.1.2: 이제 한 번에 둘 이상의 헤더가 전송되는 것을 방지합니다. 이것은 헤더 주입 공격에 대한 보호입니다. |
더 많은 예
예시
생성된 PDF 파일을 저장하라는 메시지를 사용자에게 표시합니다(Content-Disposition 헤더는 권장 파일 이름을 제공하고 브라우저가 저장 대화 상자를 표시하도록 강제하는 데 사용됨).
<?php
header("Content-type:application/pdf");
// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename='downloaded.pdf'");
// The PDF source is in original.pdf
readfile("original.pdf");
?>
<html>
<body>
...
...
❮ PHP 네트워크 참조