>  기사  >  백엔드 개발  >  PHP 헤더 기능은 무엇을 합니까? PHP 헤더 기능 소개(코드 포함)

PHP 헤더 기능은 무엇을 합니까? PHP 헤더 기능 소개(코드 포함)

不言
不言원래의
2018-07-23 16:01:591599검색

PHP 헤더 기능은 어떤 역할을 하나요? PHP에서 헤더 함수는 상태 코드를 전송하기 위해 두 개의 특수 헤더로 사용될 수 있습니다. 이는 동일한 유형의 이전 헤더를 대체하거나 HTTP 응답 값을 강제로 지정할 수 있습니다. 구체적인 내용.

먼저 공식 문서의 정의를 살펴보겠습니다

(PHP 4, PHP 5 , PHP 7)# 🎜🎜#

header — 네이티브 HTTP 헤더 보내기

1 void header ( string $string [, bool $replace = true [, int $http_response_code ]] )
매개변수: # 🎜🎜##🎜 🎜#

string

에는 두 개의 특수 헤드가 있습니다. "HTTP/

"(대소문자는 중요하지 않음)으로 시작하는 첫 번째 코드는 전송될 HTTP 상태 코드를 계산하는 데 사용됩니다. 예를 들어 존재하지 않는 파일에 대한 요청을 처리하기 위해 Apache 서버에서 PHP 스크립트를 사용하는 경우(

ErrorDocument 지시어 사용) 스크립트가 올바른 상태 코드로 응답하기를 바랍니다.

1 <?php
2 header("HTTP/1.0 404 Not Found");
3 ?>
두 번째 특수 사례는 "Location:" 헤더 정보입니다. 브라우저에 메시지를 보낼 뿐만 아니라 상태 코드가 201#🎜🎜로 설정되지 않은 한

REDIRECT

(302) 상태 코드를 브라우저에 반환합니다. #또는 3xx.

1 <?php
2 header("Location: http://www.example.com/"); /* Redirect browser */
3 
4 /* Make sure that code below does not get executed when we redirect. */
5 exit;
6 ?>
 replace

선택 매개변수
    replace
  • 동일한 유형의 이전 헤더를 이후 헤더로 바꿀지 여부를 나타냅니다. 기본적으로 대체됩니다.

    FALSE을 전달하면 동일한 헤더 정보가 강제로 공존할 수 있습니다. 예: replace 表明是否用后面的头替换前面相同类型的头。 默认情况下会替换。如果传入 FALSE,就可以强制使相同的头信息并存。例如:

1 <?php
2 header(&#39;WWW-Authenticate: Negotiate&#39;);
3 header(&#39;WWW-Authenticate: NTLM&#39;, false);
4 ?>

http_response_code

强制指定HTTP响应的值。注意,这个参数只有在报文字符串(string

 <?php
header(&#39;HTTP/1.1 200 OK&#39;); // ok 正常访问
header(&#39;HTTP/1.1 404 Not Found&#39;); //通知浏览器 页面不存在
header(&#39;HTTP/1.1 301 Moved Permanently&#39;); //设置地址被永久的重定向 301
header(&#39;Location: http://www.ithhc.cn/&#39;); //跳转到一个新的地址
header(&#39;Refresh: 10; url=http://www.ithhc.cn/&#39;); //延迟转向 也就是隔几秒跳转
header(&#39;X-Powered-By: PHP/6.0.0&#39;); //修改 X-Powered-By信息
header(&#39;Content-language: en&#39;); //文档语言
header(&#39;Content-Length: 1234&#39;); //设置内容长度
header(&#39;Last-Modified: &#39;.gmdate(&#39;D, d M Y H:i:s&#39;, $time).&#39; GMT&#39;); //告诉浏览器最后一次修改时间
header(&#39;HTTP/1.1 304 Not Modified&#39;); //告诉浏览器文档内容没有发生改变
 
###内容类型###
header(&#39;Content-Type: text/html; charset=utf-8&#39;); //网页编码
header(&#39;Content-Type: text/plain&#39;); //纯文本格式
header(&#39;Content-Type: image/jpeg&#39;); //JPG、JPEG 
header(&#39;Content-Type: application/zip&#39;); // ZIP文件
header(&#39;Content-Type: application/pdf&#39;); // PDF文件
header(&#39;Content-Type: audio/mpeg&#39;); // 音频文件 
header(&#39;Content-type: text/css&#39;); //css文件
header(&#39;Content-type: text/javascript&#39;); //js文件
header(&#39;Content-type: application/json&#39;); //json
header(&#39;Content-type: application/pdf&#39;); //pdf
header(&#39;Content-type: text/xml&#39;); //xml
header(&#39;Content-Type: application/x-shockw**e-flash&#39;); //Flash动画
 
######
 
###声明一个下载的文件###
header(&#39;Content-Type: application/octet-stream&#39;);
header(&#39;Content-Disposition: attachment; filename="ITblog.zip"&#39;);
header(&#39;Content-Transfer-Encoding: binary&#39;);
readfile(&#39;test.zip&#39;);
######
 
###对当前文档禁用缓存###
header(&#39;Cache-Control: no-cache, no-store, max-age=0, must-revalidate&#39;);
header(&#39;Expires: Mon, 26 Jul 1997 05:00:00 GMT&#39;);
######
 
###显示一个需要验证的登陆对话框### 
header(&#39;HTTP/1.1 401 Unauthorized&#39;); 
header(&#39;WWW-Authenticate: Basic realm="Top Secret"&#39;); 
######
 
 
###声明一个需要下载的xls文件###
header(&#39;Content-Disposition: attachment; filename=ithhc.xlsx&#39;);
header(&#39;Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet&#39;);
header(&#39;Content-Length: &#39;.filesize(&#39;./test.xls&#39;)); 
header(&#39;Content-Transfer-Encoding: binary&#39;); 
header(&#39;Cache-Control: must-revalidate&#39;); 
header(&#39;Pragma: public&#39;); 
readfile(&#39;./test.xls&#39;); 
######
?>

http_response_code

Force the value of the HTTP 응답. 이 매개변수는 메시지 문자열(string)이 비어 있지 않은 경우에만 유효합니다.


헤더 기능의 일반적인 용도는 다음과 같습니다.

1. 리디렉션

헤더('위치: http://www.example.com/');

2. 콘텐츠 지정:

Header('콘텐츠 유형: application/pdf');

3. 첨부 파일:

                 header('Content-type: application/pdf') 🎜🎜#

   // 내용을 다음과 같이 지정합니다. 첨부 파일을 만들고 다운로드 표시 이름을 지정합니다.

 header('Content-Disposition: attachment; filename="downloaded.pdf "');

#🎜 🎜#   //파일을 열고 출력

  readfile('original.pdf' ); caching

Header("Cache-Control: no-cache, must-revalidate") // HTTP/1.1#🎜🎜 # Header("만료: 토요일, 1997년 7월 26일 05:00:00 GMT"); // 중요한 시간 설정

자세한 예: #🎜 🎜#

rrreee# 🎜🎜# 관련 추천 :

php 헤더 함수 상세 설명

PHP 헤더() 함수 사용

위 내용은 PHP 헤더 기능은 무엇을 합니까? PHP 헤더 기능 소개(코드 포함)의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.