PHP.nl

header

header

Send a raw HTTP header

void **header** string $header bool $replace int $response_code
is used to send a raw 

header. See the for more information on headers. headerHTTP/1.1 specification

Remember that must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with , or , functions, or another file access function, and have spaces or empty lines that are output before is called. The same problem exists when using a single PHP/HTML file.

`headerincluderequire``header````php

```

headerThe header string.

   There are two special-case header calls.  The first is a header
   that starts with the string "" (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  directive), you may want to
   make sure that your script generates the proper status code.
  `HTTP/``ErrorDocument`


   
  ```php




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


       
      ```php
<?php
header("Location: http://www.example.com/"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>

replace The optional 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. For example: replace

  ```php



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



return.void


   On failure to schedule the header to be sent, 
   issues an  level error.
  `header``E_WARNING`


   
  **Voorbeeld: Download dialog**


     If you want the user to be prompted to save the data you are
     sending, such as a generated PDF file, you can use the  header to
     supply a recommended filename and force the browser to display the
     save dialog.
    Content-Disposition

```php
<?php
// We'll be outputting a PDF
header('Content-Type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf
readfile('original.pdf');
?>

Voorbeeld: Caching directives

PHP scripts often generate dynamic content that must not be cached by the client browser or any proxy caches between the server and the client browser. Many proxies and clients can be forced to disable caching with:

<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>
> **Opmerking:** > You may find that your pages aren't cached even if you don't
   output all of the headers above. There are a number of options
   that users may be able to set for their browser that change its
   default caching behavior. By sending the headers above, you should
   override any settings that may otherwise cause the output of your
   script to be cached.


   Additionally,  and
   the  configuration
   setting can be used to automatically generate the correct
   caching-related headers when sessions are being used.
  `session_cache_limiter``session.cache_limiter`

Voorbeeld: Setting a cookie

  provides a convenient way to set cookies.
 To set a cookie that includes attributes which 
 doesn't support,  can be used.
`setcookie``setcookie``header`


 For example, the following sets a cookie that includes a
  attribute.
`Partitioned`
<?php
header('Set-Cookie: name=value; Secure; Path=/; SameSite=None; Partitioned;');
?>

Opmerking: > You can use output buffering to get around this problem, with the overhead of all of your output to the browser being buffered in the server until you send it. You can do this by calling and in your script, or setting the configuration directive on in your php.ini or server configuration files. ob_start``ob_end_flush``output_buffering

Opmerking: > The HTTP status header line will always be the first sent to the client, regardless of the actual call being the first or not. The status may be overridden by calling with a new status line at any time unless the HTTP headers have already been sent. header``header

Opmerking: > Most contemporary clients accept relative s as argument to , but some older clients require an absolute URI including the scheme, hostname and absolute path. You can usually use ,

and  to make an absolute URI from a
relative one yourself:

Location:`$_SERVER['HTTP_HOST']$_SERVER['PHP_SELF']dirname````php

Opmerking: > Session ID is not passed with Location header even if is enabled. It must by passed manually using constant. session.use_trans_sidSID

headers_sent``setcookie``http_response_code``header_remove``headers_listHTTP authentication