|
header
Send a raw HTTP header
(PHP 4, PHP 5)
header() is used to send a raw HTTP header. See the » HTTP/1.1 specification for more information on HTTP headers. Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file. <html> Parameters
ChangeLog
ExamplesExample 1600. Download dialogIf 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 Example 1601. Caching directivesPHP 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
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 Notes
Note:
As of PHP 4, you can use output buffering to get around this problem,
with the overhead of all of your output to the browser being buffered
in the server until you send it. You can do this by calling
ob_start() and ob_end_flush()
in your script, or setting the
Note:
The HTTP status header line will always be the first sent to the client, regardless of the actual header() call being the first or not. The status may be overridden by calling header() with a new status line at any time unless the HTTP headers have already been sent.
Note:
There is a bug in Microsoft Internet Explorer 4.01 that prevents this from working. There is no workaround. There is also a bug in Microsoft Internet Explorer 5.5 that interferes with this, which can be resolved by upgrading to Service Pack 2 or later.
Note:
If safe mode is enabled the
uid of the script is added to the
Note:
HTTP/1.1 requires an absolute URI as argument to
» Location:
including the scheme, hostname and absolute path, but
some clients accept relative URIs. You can usually use
<?php
Note:
Session ID is not passed with Location header even if session.use_trans_sid is
enabled. It must by passed manually using
See Also
Related Examples ( Source code ) » header Examples ( Source code ) » Send email with CC and BCC Examples ( Source code ) » Authenticate user Examples ( Source code ) » Prompt Browser password dialog Examples ( Source code ) » Open browser password dialog and authenticate user based on database Examples ( Source code ) » Cookie based login form and get last login time Examples ( Source code ) » File based login Examples ( Source code ) » Use Php class to control the HTML page font Examples ( Source code ) » The Table and HTMLTable Classes Examples ( Source code ) » The Table Class Examples ( Source code ) » Mail cc and bcc Examples ( Source code ) » Mail to multiple recipients Examples ( Source code ) » String Concatenation Operators Examples ( Source code ) » Pad a string to a certain length with another string Examples ( Source code ) » Draw Filled Arc Examples ( Source code ) » Drawing a Circle Code Examples / Notes » headeranonymous
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 Languagecheckdnsrr 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 |