Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Network Functions : header

header

Send a raw HTTP header (PHP 4, PHP 5)
void header ( string string [, bool replace [, int http_response_code]] )

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.

<?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');
?>

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:

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

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 the session.cache_limiter configuration setting can be used to automatically generate the correct caching-related headers when sessions are being used.


Related Examples ( Source code ) » header
















Code Examples / Notes » header

anonymous

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.


s_donk_donk

the things from moehbass at gmail dot com is very useful.. thx!
but 1 thing i noticed, as written:
if(isset($_POST['ok'])) {
   if(isset($_POST['yesNo']))
       header('Location: http://www.google.com/');
   else
       header('Location: http://www.yahoo.com/');
}
You can see it live at:
http://labella-pizza.com/header.php
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! ^^


bholbrook

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


ik

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'.


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
Content-disposition: attachmet
and
Cache-control: no-cache
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.
Greets
 Mik


aleks

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.
To cut a long story short, PHP-generated JS and CSS files were still being reported by the headers as text/html, while in the HTML they were text/css and application/javascript; Firefox having been told the page was XHTML 1.0 became anal-retentive and refused to style the page. (I think the JS still worked but I fixed it anyway.)
Solution:
header('Content-type: text/css');
and
header('Content-type: application/javascript');


dylan

A quick way to make redirects permanent or temporary is to make use of the $http_response_code parameter in header().
<?php
// 301 Moved Permanently
header("Location: /foo.php",TRUE,301);
// 302 Found
header("Location: /foo.php",TRUE,302);
header("Location: /foo.php");
// 303 See Other
header("Location: /foo.php",TRUE,303);
// 307 Temporary Redirect
header("Location: /foo.php",TRUE,307);
?>
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


Change Language


Follow Navioo On Twitter
checkdnsrr
closelog
debugger_off
debugger_on
define_syslog_variables
dns_check_record
dns_get_mx
dns_get_record
fsockopen
gethostbyaddr
gethostbyname
gethostbynamel
getmxrr
getprotobyname
getprotobynumber
getservbyname
getservbyport
header
headers_list
headers_sent
inet_ntop
inet_pton
ip2long
long2ip
openlog
pfsockopen
setcookie
setrawcookie
socket_get_status
socket_set_blocking
socket_set_timeout
syslog
eXTReMe Tracker