How to Create PHP Files With Headers

You can use PHP to send raw HTTP headers as a visitor loads a Web page on your server. HTTP headers can be used for a range of helpful purposes, such as redirecting a user to another website, specifying the content type of the page or handling invalid URLs. The PHP function "header()" provides HTTP header functionality.

Instructions

    • 1

      Open your PHP file in a text editor, such as Windows Notepad.

    • 2

      Add one or more headers at the very beginning of the PHP file, before any spacing, HTML tags or any other code. Use the form "header(string)" or "header(string, replace, code);". The optional "replace" parameter specifies whether the header should overwrite any previously declared headers of the same type. The default value for "replace" is true. The optional "code" parameter lets you provide a HTTP response code for the header.

      For example:

      <?php header("Status: 200"); ?>

    • 3

      Use the special "location" header in the format of "header("Location: http://www.otherwebsite.com");" if you want to automatically redirect the browser to a different URL. A "REDIRECT(302)" code is also sent to the browser, which indicates a temporary redirection instead of a permanent one.

    • 4

      Use the special "HTTP/" header to make the server send a specific HTTP status code. For example, you can use "header("HTTP/1.0 404 Not Found");" to properly handle missing file requests on some servers.

    • 5

      Save the PHP file.

Tips & Warnings

  • PHP code must be contained within "<?php" and "?>" tags.

  • Even one space before your header code will cause it to not work.

Related Searches:

References

Comments

You May Also Like

Related Ads

Featured