Headers_sent, headers_list, header_remove instructions for use, headerssent
1. header
void header ( string $string [, bool $replace = true [, int $http_response_code ]] )
Function:Send a custom http packet.
Note that header () must be called before any actual output, whether it is a common html Tag, a blank line in the file, a space, or a blank line or space in the PHP file. This is a very common error. When using include, require, or its function to access other files, if there is a space or blank line before the header () is called. If other files are not called, but only a PHP or HTML file is used separately, an error occurs before the header () is called.
Parameter description:
String message string
If replace is true, it indicates that the same type of message is followed to replace the previous similar message information. The default value is true. If it is set to false, the same message information can coexist.
Http_response_code forcibly specifies the HTTP response value. Note that this parameter is valid only when the message string is not empty.
Example:Forcibly specify the redefinition 302 as 303
<?phpheader('location:http://www.example.com/', true, 303);?>
2. headers_sent
bool headers_sent ([ string &$file [, int &$line ]] )
Function:Check whether the HTTP header is sent and where it is sent.
Parameter description:
If this parameter is set, the php source file name output by the header is saved to the file variable.
Line if this parameter is set, the line number of the php source file that runs the header output is saved to the line variable.
Example:
Example: <? Phpheader ('content-type: text/html; charset = UTF-8 '); echo 'fdipzone <br>'; ob_end_flush (); if (headers_sent ($ file, $ line )) {echo "header send in $ file on line $ line";} else {echo 'not header response' ;}?>Output in the previous example: header send in/home/fdipzone/demo. php on line 5
3. headers_list
array headers_list ( void )
Function:List all header outputs (or prepare output) and return as an array
Example:Output header list
<?phpheader('content-type:text/html;charset=utf-8');header('access-control-allow-origin:*');$headers_list = headers_list();print_r($headers_list);?>
Output:
Array( [0] => X-Powered-By: PHP/5.4.3 [1] => content-type:text/html;charset=utf-8 [2] => access-control-allow-origin:*)
4. header_remove
void header_remove ([ string $name ] )
Function:Remove a header output
Parameter description:
Name header name to be removed
Example:Determine whether access-control-allow-origin: * exists. If yes, remove it.
<?phpheader('content-type:text/html;charset=utf-8');header('access-control-allow-origin:*');if(in_array('access-control-allow-origin:*', headers_list())){ header_remove('access-control-allow-origin');}print_r(headers_list());?>