>  기사  >  백엔드 개발  >  PHP 헤더의 역할

PHP 헤더의 역할

angryTom
angryTom원래의
2019-08-23 13:26:213851검색

PHP 헤더의 역할

이 글은 PHP 헤더 기능의 몇 가지 주요 기능을 주로 소개하고 있으니 많은 도움이 되셨으면 좋겠습니다.

추천 튜토리얼: PHP 비디오 튜토리얼

공식 보기 첫 번째 문서 정의

(PHP 4, PHP 5, PHP 7)

header — 기본 HTTP 헤더 보내기

void header ( string $string [, bool $replace = true [, int $http_response_code ]] )
#🎜 두 개의 특별한 머리가 있습니다. "HTTP/"로 시작하는 첫 번째 코드(대소문자는 중요하지 않음)는 전송될 HTTP 상태 코드를 계산하는 데 사용됩니다. 예를 들어 존재하지 않는 파일에 대한 요청을 처리하기 위해 Apache 서버에서 PHP 스크립트를 사용하는 경우(ErrorDocument 지시어 사용) 스크립트가 올바른 상태 코드로 응답하기를 바랍니다.

<?php
     header("HTTP/1.0 404 Not Found");
?>
두 번째 특수 사례는 "Location:" 헤더 정보입니다. 상태 코드가 미리 201 또는 3xx로 설정되어 있지 않으면 메시지를 브라우저에 보낼 뿐만 아니라 REDIRECT(302) 상태 코드를 브라우저에 반환합니다.
<?php
header("Location: http://www.example.com/"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>
replace

선택적 매개변수인 교체는 동일한 유형의 이전 헤더를 이후 헤더로 바꿀지 여부를 나타냅니다. 기본적으로 대체됩니다. FALSE가 전달되면 동일한 헤더 정보가 강제로 공존할 수 있습니다. 예:

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

http_response_code

HTTP 응답 값을 강제로 적용합니다. 이 매개변수는 메시지 문자열(string)이 비어 있지 않은 경우에만 유효합니다.

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

 1.

Header('위치: http://www.example.com/');2. :

  header('콘텐츠 유형: 애플리케이션/pdf')    헤더('콘텐츠 유형: 애플리케이션/pdf'); download.pdf"');
    //파일을 열어서 출력

  readfile('original.pdf');

위 코드 브라우저에서 파일 대화 상자 효과를 생성할 수 있습니다  

4. 사용자가 캐싱 대신 최신 정보와 데이터를 얻을 수 있도록 허용

# 🎜🎜# header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1

header("만료일: 1997년 7월 26일 토요일 05:00:00 GMT"); #🎜🎜 #

<?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;); 
######
?>

위 내용은 PHP 헤더의 역할의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

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