Send header to browser with PHP

Date: March 12th, 2007
Author: Ivory Morhuld

This tutorial will show you how to send header to the browser with PHP.

Headers is the first part of http communication which receive client web browser. To send header use:

  1. <?php
  2. header("your heaader code: goes here");
  3. ?>

If you want to send header for redirecting the browser use this:

  1. <?php
  2. header("Location:");
  3. ?>

For sending ”missing page” header try this:

  1. <?php
  2. header("HTTP/1.0 404 Not Found");
  3. ?>

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:

  1. <?php
  2. header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  3. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
  4. ?>

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 Content-Disposition header to supply a recommended filename and force the browser to display the save dialog.

  1. <?php
  2. header('Content-type: application/pdf');
  3. header('Content-Disposition: attachment; filename="document.pdf"');
  4. readfile('document.pdf');
  5. ?>

To send authentication dialog to user use this code:

  1. <?php
  2. header("WWW-Authenticate: NTLM");
  3. ?>
VN:F [1.9.18_1163]
Rating: 0.0/10 (0 votes cast)

One Response to “Send header to browser with PHP”

Leave a Reply