Summary of Different Uses of header in php Explanation of of Examples
- 2021-08-21 19:59:05
- OfStack
1. The header () function sends an original HTTP header [Http Header] to the client.
header(string,replace,http_response_code)
/*string : Required. Specifies the header string to send.
replace : Optional. Indicates whether the header replaces the previous header, or adds the 2 A header.
The default is true (Replace). false Multiple headers of the same type are allowed.
http_response_code : Optional. Put HTTP The response code is forced to the specified value. */
Note: The header () function must be called before any actual output is sent.
2. Usage 1: Jump page
header("Location:https://baidu.com"); // Normal jump
header('Refresh: 3; url=https://www.baidu.com'); //3s Backward jump
// In header When making a jump , Prevent the code from continuing to execute after an error occurs, 1 Add a exit;
Usage 2: Declare content-type (I often use it to resolve garbled codes)
header('content-type:text/html;charset=utf-8');
Usage 3: Return the response status code
header('HTTP/1.1 403 Forbidden');
Usage 4: Perform a download operation (hide the location of the file)
header('Content-Type: application/octet-stream'); // Setting content types
header('Content-Disposition: attachment; filename="example.zip"');// Settings MIME User as attachment
header('Content-Transfer-Encoding: binary'); // Set the transmission mode
header('Content-Length: '.filesize('example.zip')); // Set content length
Usage 5: Control browser cache
header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' ); // If the web page on the server changes frequently, set it to -1 Which means that it expires immediately
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
header( 'Cache-Control: no-store, no-cache, must-revalidate' );
header( 'Cache-Control: post-check=0, pre-check=0', false );
header( 'Pragma: no-cache' );
Usage 6:
3. More examples
<?php
// ok
header('HTTP/1.1 200 OK');
// Settings 1 A 404 Head :
header('HTTP/1.1 404 Not Found');
// Set the address to be permanently redirected
header('HTTP/1.1 301 Moved Permanently');
// Go to 1 New address
header('Location: http://www.example.org/');
// File delay turning :
header('Refresh: 10; url=http://www.example.org/');
print 'You will be redirected in 10 seconds';
// Of course, you can also use the html Syntax implementation
// <meta http-equiv="refresh" content="10;http://www.example.org/ />
// override X-Powered-By: PHP:
header('X-Powered-By: PHP/4.4.0');
header('X-Powered-By: Brain/0.6b');
// Document language
header('Content-language: en');
// Tell the browser that the last 1 Time of second modification
$time = time() - 60; // or filemtime($fn), etc
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');
// Tell the browser that the document content has not changed
header('HTTP/1.1 304 Not Modified');
// Set content length
header('Content-Length: 1234');
// Set to 1 Download types
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="example.zip"');
header('Content-Transfer-Encoding: binary');
// load the file to send:
readfile('example.zip');
// Disable caching for the current document
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Pragma: no-cache');
// Setting content types :
header('Content-Type: text/html; charset=iso-8859-1');
header('Content-Type: text/html; charset=utf-8');
header('Content-Type: text/plain'); // Plain text format
header('Content-Type: image/jpeg'); //JPG Picture
header('Content-Type: application/zip'); // ZIP Documents
header('Content-Type: application/pdf'); // PDF Documents
header('Content-Type: audio/mpeg'); // Audio file
header('Content-Type: application/x-shockwave-flash'); //Flash Animation
// Display login dialog box
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="Top Secret"');
print 'Text that will be displayed if the user hits cancel or ';
print 'enters wrong login data';
?>