header

Send a raw HTTP header

Syntax

header ( string $header [, bool $replace = TRUE [, int $http_response_code ]] ) : void

Parameters

header

The header string.

There are two special-case header calls. The first is a header that starts with the string "HTTP/" (case is not significant), which will be used to figure out the HTTP status code to send. For example, if you have configured Apache to use a PHP script to handle requests for missing files (using the ErrorDocument directive), you may want to make sure that your script generates the proper status code.

<?

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

?>

The second special case is the "Location:" header. Not only does it send this header back to the browser, but it also returns a REDIRECT (302) status code to the browser unless the 201 or a 3xx status code has already been set.

<?

header("Location: https://mydomain.com"); // redirect browser

exit; // make sure the code below does not get executed after the redirect

?>
replace

The optional replace parameter indicates whether the header should replace a previous similar header, or add a second header of the same type. By default it will replace, but if you pass in FALSE as the second argument you can force multiple headers of the same type.

<?

header("WWW-Authenticate: Negotiate");
header("WWW-Authenticate: NTLM", false);

?>
http_response_code

Forces the HTTP response code to the specified value. Note that this parameter only has an effect if the header is not empty.

Return

No value is returned.

Examples

header

<?

header("location: https://osbo.com");

exit;

?>

			
		

replace

<?

header("cache-control: no-cache");
header("cache-control: must-revalidate", false);

print_r(headers_list());

?>
Array
(
    [0] => X-Powered-By: PHP/8.0.10
    [1] => cache-control: no-cache
    [2] => cache-control: must-revalidate
)

http_response_code

<?

header("location: https://osbo.com", true, 301);

exit;

?>

			
		

1

<?

header("content-type: application/pdf");
header("content-disposition: attachment; filename='download.pdf'");

print_r(headers_list());

?>
Array
(
    [0] => X-Powered-By: PHP/8.0.10
    [1] => content-type: application/pdf
    [2] => content-disposition: attachment; filename='download.pdf'
)

2

<?

header("cache-control: no-cache, must-revalidate");
header("expires: 0");

print_r(headers_list());

?>
Array
(
    [0] => X-Powered-By: PHP/8.0.10
    [1] => cache-control: no-cache, must-revalidate
    [2] => expires: 0
)

PHP

Home Menu