Use PHP header() function to send HTTP headers to client

TIL the header() function in PHP that sends raw HTTP headers to a client.

header() must be called before any actual output is sent (or called), either by normal HTML tags, blank lines in a file or from PHP. For example:

<?php
    header("X-Content-Type-Options: nosniff");
    header("X-Frame-Options: DENY");
    header("X-XSS-Protection: 1; mode=block");
    header("Referrer-Policy: no-referrer-when-downgrade");
?>
<!doctype html>
<html lang="en">
<head>
...

Syntax

void header ( string $string [, bool $replace = true [, int $http_response_code ]] )
Parameter Description
string required - Specifies the header string to send
replace optional - Indicates whether the header should replace previous or add a second header; default is true (will replace); false (allows multiple headers of the same type)
http_response_code optional - Forces the HTTP response code to the specified value

Documentation

See the documentation for more details.


SHARE THIS ARTICLE