TL;DR
PHP's header function sends an HTTP header to the browser. With it you can redirect to a new location, control the content type so a file like css.php is treated as CSS, force a download prompt with a suggested filename, send specific errors, and then call exit so no later code runs.
- A location header redirects the browser immediately. A full URL is recommended, though almost all browsers support relative URLs.
- Serving css.php with a CSS content type lets you build a style sheet dynamically based on the viewer's browser or resolution.
- An attachment content-disposition shows the download prompt with a filename like file.jpg, while switching it to inline displays the file in the page.
This is an archived tutorial from the kirupa.com legacy collection. It covers software that may no longer be available, but it is kept online because the ideas still hold up.
I've needed the header function in php so many times I figured I'd write a tutorial on it. This tutorial should help you do most of the things that the header function is capable of. Simply put, the php header function will send a http header to the browser.
Here is How
The following steps should teach you everything you need to know about php headers.
-
The http header allows you to send many messages to the browser, the first of these that I'll explain is the location header.
<?php
header("Location: http://kirupa.com");
?>
-
This will send a new location to the browser and it will immediately redirect. It's recommended to put a full url there, however almost all browsers support relative urls.
-
You can also control the content type that the browser will treat the document as:
<?php
header("Content-Type: text/css");
?>
-
You can now link to this file as css.php in your link to your css and dynamically create your css document depending on what browser or resolution the viewer has. This can be really helpful when designing css that works in every browser.
-
You can also force the browser to display the download prompt and have a recommended filename for the download.
<?php
header("Content-Type: image/jpeg");
header("Content-Disposition: attachment; filename=file.jpg");
?>
-
This will not show the file as it usually would in a browser, but as I mentioned before, display the downloads prompt and the filename will automatically be set to file.jpg regardless the filename of the php file. You can also force the page to be displayed inline by changing the content-disposition from attachment to inline.
- You can also send specific errors to the browser using the header function. It's important to remember the different error messages and what they mean.
<?php
header("HTTP/1.0 404 Not Found");
?>
- Finally, I'd like to suggest that immediately following using a header, you use exit to make sure none of the code after it is executed (unless of course that code is used to make an image or needed in the file):
<?php
header("Location: http://www.example.com/");
exit;
?>
If you need any help with this tutorial, I suggest before asking on the forums, looking at php's documentation of the function here. If you can't find what you're looking for there, feel free to post on the forums.
Cheers!
|
|
Jeff Wheeler Nokrev.com |
Before you go, here are a few things you can do:
-
Keep reading
Read the next article in the series, Hello World, or check out some related content:
-
Ask a question
Stuck on something, or curious about a detail we skipped? Ask on the forums, and we’ll help you out.
Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence, ads, and algorithm-driven doodads. A huge thank you to all of you who buy kirupa's books, became a paid subscriber, watch the videos, and/or interact on the forums.
Your support keeps this site going! 😇
