Send a raw HTTP header
(PHP 4, PHP 5)
Example 1600. 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 » Content-Disposition header to supply a recommended filename and force the browser to display the save dialog.
Example 1601. 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:
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, session_cache_limiter() and
Related Examples ( Source code ) » header
Code Examples / Notes » header
When streaming a PDF file I would get the error "'There was an error opening this document. This file cannot be found." This seemed to only happen in IE6 that I'm aware of.
After changing the Content-Disposition to inline (rather then attachment) it worked properly.
the things from moehbass at gmail dot com is very useful.. thx!
but 1 thing i noticed, as written:
You can see it live at:
it works fine, only if u put the php on the top.
when u open the link above, u can copy paste the code included in the textarea to try it..
BUT when u run it, it didnt work.
as u can see, the php code is below the html code.
u have 1 simple thing to do :
cut the php code and paste it at the top.. before the html code..
and run it...
then u'll find it works fine! ^^
The first element of the header (i.e. "Location") is case sensitive depending on the browser. In IE7, <?php header("location:http://www.url.com"); ?> does not work as expected whereas <?php header ("Location:http://www.ulr.com"); ?> does work as expected.
For a full list of headers and their values go here: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
If you want your download script to work in Safari you'll have to print quotation marks around the filename:
header('Content-Disposition: attachment; filename="'.$fileName.'"');
,otherwise Safari will just save the file as 'scriptname.php'.
If you want to serve files to be opend in a separate application (also PDF files displayed in the Reader and not in the IE Plugin) with the dialog open/save as/cancel :
Don't use the
header at same time.
Normaly the browser stores the file in a temporary directory and starts the default application for the mime-type with the path of the file as parameter.
Problem : IE6 doesn't store because of no-cache. So the application (in my case AcrobatReader) displays the file can't be found.
I recently had a hair-threatening problem with Firefox and XHTML 1.0 transitional.
It worked fine with other browsers, and also with HTML 4.1.
A quick way to make redirects permanent or temporary is to make use of the $http_response_code parameter in header().
// 301 Moved Permanently
// 302 Found
// 303 See Other
// 307 Temporary Redirect
The HTTP status code changes the way browsers and robots handle redirects, so if you are using header(Location:) it's a good idea to set the status code at the same time. Browsers typically re-request a 307 page every time, cache a 302 page for the session, and cache a 301 page for longer, or even indefinitely. Search engines typically transfer "page rank" to the new location for 301 redirects, but not for 302, 303 or