search
HomeBackend DevelopmentPHP TutorialDetailed explanation of PHP header function setting http message header example

<span>//定义编码
header( <span>'Content-Type:text/html;charset=utf-8 ');
 
<span>//Atom
header(<span>'Content-type: application/atom+xml');
 
<span>//CSS
header(<span>'Content-type: text/css');
 
<span>//Javascript
header(<span>'Content-type: text/javascript');
 
<span>//JPEG Image
header(<span>'Content-type: image/jpeg');
 
<span>//JSON
header(<span>'Content-type: application/json');
 
<span>//PDF
header(<span>'Content-type: application/pdf');
 
<span>//RSS
header(<span>'Content-Type: application/rss+xml; charset=ISO-8859-1');
 
<span>//Text (Plain)
header(<span>'Content-type: text/plain');
 
<span>//XML
header(<span>'Content-type: text/xml');
 
<span>//200 OK
header(<span>'HTTP/1.1 200 OK');
 
<span>//设置一个404头:
header(<span>'HTTP/1.1 404 Not Found');
 
<span>//设置地址被永久的重定向
header(<span>'HTTP/1.1 301 Moved Permanently');
 
<span>//转到一个新地址
header(<span>'Location: http://www.example.org/');
 
<span>//文件延迟转向:
header(<span>'Refresh: 10; url=http://www.example.org/');
<span>print <span>'You will be redirected in 10 seconds';
 
<span>//当然,也可以使用html语法实现
<span>//<meta http-equiv="refresh" c>//override X-Powered-By: PHP:
header(<span>'X-Powered-By: PHP/4.4.0');
header(<span>'X-Powered-By: Brain/0.6b');
 
<span>//文档语言
header(<span>'Content-language: en');
 
<span>//告诉浏览器最后一次修改时间
<span>$time = time() - <span>60; <span>// or filemtime($fn), etc
header(<span>'Last-Modified: '.gmdate(<span>'D, d M Y H:i:s', <span>$time).<span>' GMT');
 
<span>//告诉浏览器文档内容没有发生改变
header(<span>'HTTP/1.1 304 Not Modified');
 
<span>//设置内容长度
header(<span>'Content-Length: 1234');
 
<span>//设置为一个下载类型
header(<span>'Content-Type: application/octet-stream');
header(<span>'Content-Disposition: attachment; filename="example.zip"');
header(<span>'Content-Transfer-Encoding: binary');
<span>//load the file to send:
readfile(<span>'example.zip');
 
<span>//对当前文档禁用缓存
header(<span>'Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header(<span>'Expires: Mon, 26 Jul 1997 05:00:00 GMT'); <span>// Date in the past
header(<span>'Pragma: no-cache');
 
<span>//设置内容类型:
header(<span>'Content-Type: text/html; charset=iso-8859-1');
header(<span>'Content-Type: text/html; charset=utf-8');
header(<span>'Content-Type: text/plain'); <span>//纯文本格式
header(<span>'Content-Type: image/jpeg'); <span>//JPG***
header(<span>'Content-Type: application/zip'); <span>// ZIP文件
header(<span>'Content-Type: application/pdf'); <span>// PDF文件
header(<span>'Content-Type: audio/mpeg'); <span>// 音频文件
header(<span>'Content-Type: application/x-shockw**e-flash'); <span>//Flash动画
 
<span>//显示登陆对话框
header(<span>'HTTP/1.1 401 Unauthorized');
header(<span>'WWW-Authenticate: Basic realm="Top Secret"');
<span>print <span>'Text that will be displayed if the user hits cancel or ';
<span>print <span>'enters wrong login data';</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br>

#Attachment: HTTP header detailed explanation

Requests part

Accept-RangesAuthorization Cache-ControlConnectionCookieContent-LengthContent-TypeDateExpectFromHostIf-MatchIf-Modified- SinceIf the entity has not changed, the server Send the client's missing parts, otherwise send the entire entity. The parameter is also EtagThe request is successful only if the entity has not been modified after the specified timeLimit the time when information is transmitted through proxies and gatewaysUsed to contain implementation-specific instructionsAuthorization certificate connected to the proxyOnly request a part of the entity, specify the rangeThe address of the previous webpage, the current requested webpage is immediately followed, that is, the way to go The transfer encoding that the client is willing to accept, and notifies the server to accept the tail plus header information Specify a certain transport protocol to the server for conversion by the server (if supported) The content of User-Agent contains the user information that made the requestWarn: 199 Miscellaneous warning

Responses section

Header Explanation Example
Accept Specify the content type that the client can receive Accept: text/plain, text/html
Accept-Charset The character encoding set that the browser can accept. Accept-Charset: iso-8859-5
Accept-Encoding Specifies the content compression encoding type returned by the web server that the browser can support. Accept-Encoding: compress, gzip
Accept-Language Accept-Language: en,zh
You can request one or more web page entities Sub-range field Accept-Ranges: bytes
Authorization certificate for HTTP authorization Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
Specify the caching mechanism followed by requests and responses Cache -Control: no-cache
indicates whether a persistent connection is required. (HTTP 1.1 uses persistent connections by default) Connection: close
When an HTTP request is sent, all cookie values ​​stored in the requested domain name will be sent to the web server. Cookie: $Version=1; Skin=new;
Requested content length Content-Length: 348
The requested MIME information corresponding to the entity Content-Type: application/x-www-form-urlencoded
The date and time the request was sent Date: Tue, 15 Nov 2010 08:12:31 GMT
Specific server behavior requested Expect: 100-continue
Email of the user who made the request From: user@email.com
Specify the domain name and port of the requested server No. Host: www.zcmhi.com
Only valid if the request content matches the entity If-Match: "737060cd8c284d8af7ad3082f209582d"
if requested If the part is modified after the specified time, the request is successful. If it has not been modified, a 304 code will be returned. If the content has not changed, a 304 code is returned. The parameter is the Etag previously sent by the server. Compare it with the Etag responded by the server to determine whether it has changed. If-None-Match: "737060cd8c284d8af7ad3082f209582d" If-Range
If-Range: "737060cd8c284d8af7ad3082f209582d" If-Unmodified-Since
If-Unmodified-Since: Sat, 29 Oct 2010 19:43:31 GMT Max-Forwards
Max-Forwards: 10 Pragma
Pragma: no -cache Proxy-Authorization
Proxy-Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== Range
Range: bytes=500- 999 Referer
Referer: http://www.zcmhi.com/archives/71.html TE
TE: trailers,deflate;q=0.5 Upgrade
Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11 User-Agent
User-Agent: Mozilla/5.0 (Linux ;
Header Explanation Example
Accept-Ranges Indicates whether the server supports specified range requests and what type of segmented requests Accept-Ranges: bytes
Age Estimated time from origin server to proxy cache formation (in seconds, non-negative) Age: 12
Allow A valid request behavior for a certain network resource, if not allowed, return 405 Allow: GET, HEAD
Cache-Control Tell all caching mechanisms whether they can be cached and which types Cache-Control: no-cache
Content-Encoding The returned content supported by the web server Compression encoding type. Content-Encoding: gzip
Content-Language The language of the response body Content-Language: en,zh
Content-Length The length of the response body Content-Length: 348
Content-Location Request an alternative address for the resource Content-Location: /index.htm
Content-MD5 Return the MD5 check value of the resource Content-MD5: Q2hlY2sgSW50ZWdyaXR5IQ==
Content-Range The byte position of this part in the entire return body Content-Range: bytes 21010-47021/47022
Content-Type Return content MIME type Content-Type: text/html; charset=utf-8
Date The time when the original server message was sent Date: Tue, 15 Nov 2010 08:12:31 GMT
ETag Request The current value of the variable's entity tag ETag: "737060cd8c284d8af7ad3082f209582d"
Expires The date and time the response expires Expires: Thu, 01 Dec 2010 16:00:00 GMT
Last-Modified The last modified time of the requested resource Last-Modified: Tue, 15 Nov 2010 12:45:26 GMT
Location Used to redirect the receiver to the location of the non-requested URL to complete the request or identify a new one Resource Location: http://www.zcmhi.com/archives/94.html
Pragma includes implementation specific directives which can be applied to any receiver on the response chain Pragma: no- cache
Proxy-Authenticate It indicates the authentication scheme and parameters that can be applied to the proxy on that URL Proxy-Authenticate: Basic
refresh Applies to redirects or when a new resource is created , redirect after 5 seconds (proposed by Netscape, supported by most browsers)

Refresh: 5; url=

http://www.zcmhi.com/archives/94.html

Retry-After If the entity is temporarily unavailable, notify the client to try again after the specified time Retry-After: 120
Server web server software name Server: Apache/1.3.27 (Unix ) (Red-Hat/Linux)
Set-Cookie Set Http Cookie Set-Cookie: UserID=JohnDoe; Max-Age=3600; Version=1
Trailer Point out the header field The tail of chunked transfer-encoding exists Trailer: Max-Forwards
Transfer-Encoding File Transfer-Encoding Transfer-Encoding: chunked
Vary tells the downstream proxy whether to use a cached response or from the original Server Request Vary: *
Via tells the proxy where the client response was sent Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1)
Warning Warning Entity Possible Problems Warning: 199 Miscellaneous warning
WWW-Authenticate Indicates the authorization scheme that the client requesting entity should use WWW-Authenticate: Basic

Original text http://blog. csdn.net/wyqwclsn/article/details/41082083

The above introduces the detailed explanation of the example of setting the http message header by the PHP header function, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Optimize PHP Code: Reducing Memory Usage & Execution TimeOptimize PHP Code: Reducing Memory Usage & Execution TimeMay 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor