Php header of function syntax and usage code

  • 2020-11-03 22:03:20
  • OfStack

Grammar:


Void header(string $string[,bool $replace=true [, int $http_response_code)

Send the original HTTP header to the client
Note:
The Header function must be called before any actual output, whether it is a 1-like html tag, a hollow line in a file, or from php. You can't have any output before this function.
Parameter description:

Parameters to describe
string required. Specifies the header string to be sent.
replace optional. Indicates whether the header should replace the previous header, or add a second header.
The default is true (replace). false (allows multiple headers of the same type).

http_response_code optional. Force the HTTP response code to the specified value. (PHP 4 and later available)
Common usage examples:

1. Send server status code


header('HTTP/1.0 404 Not Found');

Common status code:

Status code description
100-199 indicates that the request was successfully received and requires the client to continue to submit the next request to complete the entire process
200-299 successfully received the request and completed the entire processing process, commonly used 200
300-399 unfinished request, the client needs to take a step to refine the request, for example, the requested resource has been moved to a new address, commonly used 302,304
400-499 client request error 404 commonly used
An error has occurred on the server side of 500-599, commonly used 500
See HTTP for details & FTP corresponding prompt message

2. Page jump


//  Immediately jump 
header('Location: https://www.ofstack.com/');

// 3 Seconds after the jump 
header('refresh:3;url=https://www.ofstack.com');

3. Set the browser cache 
 Force the browser not to cache! 
[code]
//header('Expires:-1');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Cache-Control:no-cache,must-revalidate');
header('pragma:no-cache');
header('Last-Modified: '. date('D, j M Y H:i:s T') );

Description:
The time after Expires must be in GMT format such as gmdate(" M d Y H:i ");
The common values of Cache-control include private, ES64en-ES65en, ES66en-ES67en, must-ES69en, etc., and the default value is private. Its function is divided into the following cases according to the different ways of re-browsing.
Cache - directive instructions
All public content will be cached
private content is only cached in the private cache
no-cache All contents will not be cached
All es82EN-ES83en content will not be cached in the cache or Internet temporary files
If the contents of the cache fail, the request must be sent to the server/agent for revalidation
max-age =xxx (xxx is numeric) cache contents will expire after xxx seconds, this option is only available for HTTP 1.1 and has a higher priority if used from Last-Modified1
Open a new window
If you specify cache-ES104en as private, ES106en-ES107en, must-ES109en, the server will be re-visited when a new window access is opened. If the ES110en-age value is specified, the time within this value will not be re-accessed to the server, as follows:

header('cache-control:max-age=5');

Indicates that the server will not be accessed again within 5 seconds after visiting this page

Enter in the address bar
If the value is private or ES119en-ES120en, the server will only be accessed on the first visit and will not be accessed thereafter. If the value is ES121en-ES122en, then it is accessed every time. If the value is ES123en-ES124en, the access will not be repeated until it expires.
Press the back button
If the values are private, ES128en-ES129en, ES130en-ES131en, the access will not be repeated, while if it is ES132en-ES133en, the access will be repeated each time
Press the refresh button
Whatever the value is, it will be accessed repeatedly. When the ES136en-ES137en value is specified as "ES138en-ES139en", visiting this page does not leave a copy of the page in the Internet temporary article folder. In addition, the cache is also affected by specifying the "Expires" value. For example, if you specify the value of Expires to be 1 time long past, if you repeatedly press enter in the address bar when accessing the network, you will be repeatedly visited each time:
Expires: Fri, 31 Dec 1999 16:00:00 GMT
About "Last - Modified
When the browser requests a certain URL for the first time, the server side will return a state of 200, which is the resource you requested. At the same time, there is a property of Last-ES151en to mark the time when this file was last modified in the service side. The format is similar to this:
Last-Modified: Fri, 12 May 2006 18:53:33 GMT
When the client requests this URL for the second time, according to the HTTP protocol, the browser will send the If-ES158en-ES159en header to the server, asking whether the file has been modified after that time:

If-Modified-Since: Fri, 12 May 2006 18:53:33 GMT
If the resource on the server side does not change, the HTTP 304 (Not Changed.) status code is automatically returned with empty content, thus saving the amount of data transferred. When server-side code changes or the server is restarted, the resource is reissued, returning the same as the first request. This ensures that resources are not issued repeatedly to the client and that the client can get the latest resources when the server changes.

Last-Modified improves performance
Smart developers use Last-ES173en and ETags to request the http header 1 to take advantage of the cache of the client (such as the browser). Because the server first produces the ES176en-ES177en /Etag tag, the server can use it later to determine if the page has been modified. Essentially, the client asks the server to validate its (client's) cache by passing the token back to the server.

The process is as follows:

Client requests 1 page (A)
The server goes back to page A and adds an ES187en-ES188en to A
The client renders the page and caches it along with ES190en-Modified1
The client requests the page A again and passes the es194EN-Modified1 returned by the server on the last request to the server
The server examines the ES197en-ES198en, determines that the page has not been modified since the last client request, and returns the response 304 with an empty body.

3. File download
For example, download an pdf file


// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
// Indicate that it is an attachment, name it and download it 
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in 1.pdf
readfile('1.pdf');

In fact, to better understand these!

header('content-type:application/octet-sream');
Header('Accept-Ranges:bytes');

// Return in bytes 
Header('content-disposition:attachement;filename=" The file name "');
Header('Accept-length:'.$filesize);


Related articles: