PHP 헤더 () 함수
정의 및 사용
클라이언트에 헤더 () 함수는 원시 HTTP 헤더를 보낼 수 있습니다.
어떤 실제 출력이 전송되기 전에 (PHP 4와 이후 버전에서,이 문제를 해결하기 위해 출력 버퍼링을 사용할 수 있습니다) 헤더 () 함수를 호출해야한다는 것을 인식하는 것이 중요하다 :
<html>
<?php
// This results in an error.
// The output above is before the header() call
header('Location: http://www.example.com/');
?>
<?php
// This results in an error.
// The output above is before the header() call
header('Location: http://www.example.com/');
?>
문법
header(string,replace,http_response_code)
参数 | 描述 |
---|---|
string | 必需。规定要发送的报头字符串。 |
replace | 可选。指示该报头是否替换之前的报头,或添加第二个报头。默认是 TRUE(替换)。FALSE(允许相同类型的多个报头)。 |
http_response_code | 可选。把 HTTP 响应代码强制为指定的值。(PHP 4.3 以及更高版本可用) |
팁과주의 사항
참고 : PHP 4.4 이후, 함수가 여러 헤더를 보내 방지하기 위해 시작.이것은 머리에 주입 공격에 대한 보호이다.
예 1
페이지 캐싱 사용 안 함 :
<?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>
...
...
// 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>
...
...
참고 : 사용자는 기본 브라우저 캐시 설정을 변경하는 몇 가지 옵션을 설정할 수 있습니다.위의 헤더를 전송함으로써, 사용자는 브라우저 캐시를 강요하지 않는,이 설정을 재정의 할 수 있습니다!
예 2
생성 된 PDF 파일을 저장하라는 메시지를 표시 (내용 - 처리 헤더는 권장되는 파일 이름을 제공하고 대화 저장 표시 할 브라우저를 강제하는 데 사용됩니다)
<?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>
...
...
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>
...
...
참고 : Microsoft IE 버그를 방지하는기구가 존재보다 5.5 이상.서비스 팩 2 이상 버전으로 업그레이드하여 버그를 해결할 수 있습니다.
전체 PHP HTTP 참조 설명서