★ wanayoo — archive 1999 http://www.php.net/manual/function.header.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to HTTP
Quick Reference
German version of this pageJapanese version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
HTTP
* header
* header_sent
* setcookie
Manual: header
View the source code for this pageSearch the site



Previous page
 HTTP
 Updated
Sat, 12 Aug 2000
header_sent 
Next page


header

(PHP3 , PHP4 )

header -- Send a raw HTTP header

Description

int header (string string)

The Header() function is used at the top of an HTML file to send raw HTTP header strings. See the HTTP 1.1 Specification for more information on raw http headers. Note: Remember that the Header() function must be called before any actual output is sent either by normal HTML tags or from PHP. It is a very common error to read code with include() or with auto_prepend and have spaces or empty lines in this code that force output before header() is called.

There are two special-case header calls. The first is the "Location" header. Not only does it send this header back to the browser, it also returns a REDIRECT status code to Apache. From a script writer's point of view this should not be important, but for people who understand Apache internals it is important to understand.


header ("Location: http://www.php.net"); /* Redirect browser 
                                            to PHP web site */
exit;                 /* Make sure that code below does 
                         not get executed when we redirect. */
      

The second special-case is any header that starts with the string, "HTTP/" (case is not significant). For example, if you have your ErrorDocument 404 Apache directive pointed to a PHP script, it would be a good idea to make sure that your PHP script is actually generating a 404. The first thing you do in your script should then be:


header ("HTTP/1.0 404 Not Found");
      

PHP scripts often generate dynamic HTML 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


header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
                                                      // always modified
header ("Cache-Control: no-cache, must-revalidate");  // HTTP/1.1
header ("Pragma: no-cache");                          // HTTP/1.0
      

See also headers_sent()


User Contributed Notes: header


mnot@pobox.com
18-Apr-1999 12:12
All modern browsers and proxy caches will not require setting an exlicit Expires for dynamic content; PHP's lack of validators ensure that no content will be cached.


For more information, or to find out how to make PHP content cacheable, see <a href=/old?u=http%3A%2F%2Fwww.php.net%2Fmanual%2F%26quot%3Bhttp%3A%2Fwww.pobox.com%2F~mnot%2Fcache_docs%2F%26quot%3B%26gt%3Bhttp%3A%2Fwww.pobox.com%2F~mnot%2Fcache_docs%2F%26lt%3B%2Fa%26gt%3B%3C%2Ftt&y=1999>



bill@evil.inetarena.com
28-Apr-1999 11:04

Setting the File Name in a Download Dialog.


If you want to "suggest" a file name for a download to a browser, you can do this via the header:


<pre>
header( "Content-type: application/x-gzip" );
header( "Content-Disposition: attachment; filename=some-file.tar.gz" );
header( "Content-Description: PHP3 Generated Data" );
</pre>



chopin@csone.kaist.ac.kr
27-Aug-1999 02:41
<b>Reading header of URL</b>
php does not support reading header function. you must open socket directly. Following is the function to read URL header. (see <a href=/old?u=http%3A%2F%2Fwww.php.net%2Fmanual%2F%26quot%3Bhttp%3A%2Fwww.phpwizard.net%2FphpTidbits%2F%26quot%3B%26gt%3BphpTidbits%26lt%3B%2Fa%26gt&y=1999
<PRE>
function get_headers($host, $path = "/")
{
$fp = fsockopen ("$host", 80, &$errnr, &$errstr) or die("$errno: $errstr");
fputs($fp,"GET $path HTTP/1.0\n\n");
while (!$end)
{
$line = fgets($fp, 2048);
if (trim($line) == "")
$end = true;
else
echo $line;
}
fclose($fp);
}
</PRE>



abo@minkirri.apana.org.au
27-Aug-1999 02:58
If you are just after header information using the above technique, surely it's better to use "HEAD" instead of "GET", as this will avoid transmission of all the content which doesn't need to then be striped out.



henning@sprang.de
16-Oct-1999 10:17
against the very first statement made above, that modern Browsers automatically reload dynamic content I've experienced it several times that IE5 cached dynamic pages and i had to do Shift-Reload to see how changes to my script affected the generated pages.


sba@networkers.dk
05-Nov-1999 09:07
I have experienced similar problems in IE5 with cached pages. Go to [Functions|Internet Settings|Temporary Internet Files|Settings] and change the "Check for new versions of previously cached pages" to "At every visit to the page". That will give you a little overhead initially, but will solve the problem..


simoned@eisa.net.au
19-Nov-1999 01:23
Just a note about if you are having trouble with include() files and setting
headers. Make sure that any included files before a header() statement do
*not* have blank lines at the end of the file after the close PHP tag.



ifedulov@outlook.net
01-Dec-1999 07:26
I hear somebody have problems with caching. Why then you just not put following tag into every single file in your site and everything will be fine:
<META HTTP-EQUIV="Expires" CONTENT="0">

That is it, every page will be for sure loaded from the site :)



marras@ctrl.se
03-Dec-1999 05:22
Note that using the Location header like this:
<PRE>
header("Location: http://www.php.net");
</PRE>
does not work in netscape (at least it doesnt for me). If anyone knows why, or how to do it instead, pleasae send me a mail!



something@something.net
08-Dec-1999 05:43
try header("Location: https://some.place.com/\n");



tgrubb@wallysoft.com
12-Dec-1999 02:53
Make sure that included files do *NOT* have any space at the top before the PHP tags either.


alan@holotech.net
12-Dec-1999 12:21
Outputting the full HTTP header as suggested in the documentation:

<PRE>
header("http/1.0 404 Not Found");
</PRE>

causes a 500 error, at least in the version of Apache I'm using (1.3.3). Instead, use the Status header:

<PRE>
header("Status: 404 Not Found");
</PRE>



Pascal.Dufour@cdt.eu.int
15-Dec-1999 05:32
Answer to: ifedulov@outlook.net
> I hear somebody have problems with caching. Why then you just not put following tag into every
> single file in your site and everything will be fine: <META HTTP-EQUIV="Expires" CONTENT="0"> That
> is it, every page will be for sure loaded from the site :)

Your solution will work with browser cache but not with server cache (like squid) that do not "see"
the html content. Server cache relies on HTTP headers.



david@audiogalaxy.com
20-Feb-2000 11:04
RFC 2068 (HTTP 1.1 Specification) has been superceeded by RFC 2616 http://www.w3.org/Protocols/rfc2616/rfc2616.html See section 14 for definitions of HTTP headers.


j.clements@computer.org
27-Feb-2000 04:28


When a visitor chooses a new page on your site by clicking on a link you
have set up, the link will include all the arguments required to
identify what page to (create and) send. If the visitor uses a form to
choose a page, the form variables are passed via the post method and
don't appear in the "Location" header sent to the browser with the
requested page, so if the visitor bookmarks that page, the bookmark will
lead back to your php3 file with no arguments given.

How do you put into the "Location:" header all the information that is
needed to enable the user to bookmark a particular page? This exercised
me for several days and I poured through the http 1.1 spec and the php
manual to no avail. (Just sending a new "Location" header doesn't work.)

I finally solved it through brute force with:


header ("Request-URI:
http://www.publicinfo.net/pic.php3?allvars=$allvars");

header ("Content-Location:
http://www.publicinfo.net/pic.php3?allvars=$allvars");

header ("Location:
http://www.publicinfo.net/pic.php3?allvars=$allvars");


If you send these headers in this order, it works! If you just send the
"Location" header or send it first, the browser gets a 301 Found!, which
is worse than useless.



nicolai@dufva.com
27-Feb-2000 08:39
This is not rocket science, but I add it to help others in the future.
If you are trying to use for WML pages, you obviously first need to turn off
the short tag style in the php ini file to enable XML source files, but to get
the WAP browser to accept your page, add the following as the first line
of your php file:
<pre>
&lt;?php header( "Content-type: text/vnd.wap.wml" ); ?>
</pre>

Again, it may not be rocket science, but others might find it niceto have
here in the manual (plus it makes be feel good, to contribute to this manual :-)



theo@darlings.net
06-Mar-2000 01:20
<PRE>
Okay, next person to e-mail me about the header target question I posted at
the top of this page gets throttled.

Some guy wrote me and said that you can add a Window-target specification
so it would be like
header("Location: http://www.mysite.com/ Window-target: _top");
or something like that.
He also said that it doesn't work with all browsers. I haven't tried it, so
don't come yelling to me if it doesn't work.
</PRE>



peterNOSPAM@helpnet.com.au
08-Mar-2000 05:30
For Apache 1.3.9 W32, I had to replace
header("http/1.0 404 Not Found");
header("Location: http://www.php.net");
with
header("Status 301 Moved Permanently");
header("Location: http://www.php.net");

Sorry about the NOSPAM in the email address. I currently get up to 50 unsolicted commercial email per day.



cck197@ecs.soton.ac.uk
18-Mar-2000 06:36
Here's a feature which you might want to be aware of, which has just cost me a lot of time:

I tend to use the <pre>define()/2</pre> function like C pre-processor <pre>#defines</pre>. However, if you use this function to define HTTP headers, they get interpreted immediately! Here's an example:
<pre>
define( "ARTICLE_MANAGEMENT", "Location: http://" . HOST . "/php/f1rumors/article_management.php3" );
</pre>
At the top of my script causes an automatic redirection to article_management.php3 without ever calling <pre>header()/1</pre>. Be aware.



Zmegar@easynet.frZ
28-Mar-2000 02:20
To send a binary file and tell a suggested filename to the browser, use:

<PRE>header( "Content-type: application/octet-stream" );
header( "Content-Disposition: attachment; filename=\"suggested-filename\"" );
</PRE>

Warning: the filename have to be quoted.
(RFC 2616)

(remove the 'Z' in my email to answer me)



Zmegar@easynet.frZ
28-Mar-2000 02:55
As said above, the function<PRE>
header("http/1.0 404 Not Found");
</PRE>
does not work. In fact, it only works when PHP is compiled as an Apache module (see source <PRE>head.c</PRE>)


It is better to use<PRE>
header("status: 404 Not Found");
</PRE>
Because it always works whenever php is compiled as an Apache module or a standalone binary.


(Applies to php up to 3.0.15, i have not tested the later versions)



(remove the 'Z' in my email to answer me)



joe@aigraphics.com
28-Mar-2000 08:21
here us an example of a User Auth This is tried and proven on dtheatre.com:
<pre>
if (!isset($PHP_AUTH_USER)) {
Header("WWW-Authenticate: Basic realm=\"Admin\"");
Header("HTTP/1.0 401 Unauthorized");
$REMOTE_USER = "";
$REMOTE_PASSWORD = "";
$PHP_AUTH_USER = "";
$PHP_AUTH_PW = "";
print "You hit cancel! if it was an accident please <a href=/old?u=http%3A%2F%2Fwww.php.net%2Fmanual%2F%5C%26quot%3B%24PHP_SELF%5C%26quot%3B%26gt%3Bclick&y=1999 here</a> to try again.";
exit;
} else if ((!$PHP_AUTH_USER == "user") && ($PHP_AUTH_PW == "pass")) {
print "You are unauthorized";
}
</pre>



mu@mikrobit.com.pl
04-Apr-2000 12:59
WWW-Authenticate: works fine unless you're doing it once. Calling unset($PHP_AUTH_USER) gives no success and this variable is still visible for commands like: if (!isset($PHP_AUTH_USER) ).


lists@obira.de
18-Apr-2000 09:12
After several hours of wondering why IE5.0 doesn´t display my custom
Errorpage i found the sollution in this article <a href=/old?u=http%3A%2F%2Fwww.php.net%2Fmanual%2F%26quot%3Bhttp%3A%2Fsupport.microsoft.com%2Fsupport%2Fkb%2Farticles%2Fq218%2F1%2F55.asp%26quot%3B%26gt%3BQ218155%26lt%3B%2Fa%26gt%3B.&y=1999
In short: The Errorpage needs to be larger than 512 Bytes.




fake-steve@fojar.com
26-Apr-2000 02:44
I encountered the following issue; maybe it's a known issue and I wasted a bunch of time.

If you're trying to force a page-reload using location: redirection, it won't work. you can only redirect to another page. i use one php script for a large and complicated web-based invoicing system, and ran into this problem when trying to fetch the next order from the queue when the user encountered an odd order. using location:, it would parse through to the end of the script and display a blank page, but redirecting to another one-liner that redirected back, it worked.

hope this helps someone else...



reb@wd3.com
08-May-2000 08:27
To clear out an authorized user....

If you use $PHP_AUTH_USER="" it will get rid of this problem.



webmaster@linko.com
21-May-2000 12:31
To Download files for valid name, try like this...
<pre>
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=yourfilename");
header("Content-Transfer-Encoding: binary");
</pre>
but... when use IE5.0 to open files without download must select open two times..



luciano@netsolution.com.ar
24-Jul-2000 04:55
To make a PHP script open up a windows asking to download use this code:
header("Content-Type: application/download\n");
header("Content-Disposition: attachment; filename=\"file.tgz\"");
$fn=fopen("file.tgz" , "r");
fpassthru($fn);


This works for any file..

Luciano Ramos



steven@herod.net
27-Jul-2000 01:18
Note, calls to header in a particular order do not have any affect over the order in which they are actually output to the browsers, from Rasmus Lerdorf :
"PHP can't set the order of
headers. All PHP does it fill in the response header table and Apache
decides what order to send them in."

With Apache 1.3.4 this causes the code:

header("Content-type: text/html");
header("Content-Encoding: gzip");

Actually puts out:

Content-Encoding: gzip
[snip]
Content-Type: text/html

Enjoy!



brendan@essex.ac.uk
03-Aug-2000 05:51
Contrary to the examples, I found that
the HTTP/1.0 has to be in uppercase. This is using the header() command as the first line in a PHP file pointed to by Apache's ErrorDocument directive.

Server: Apache/1.3.9 (Unix) PHP/4.0.1pl2



philliptemple@yahoo.co.uk
10-Aug-2000 01:01
To all of those wanting to use Basic Authentication and are getting 'Internal Error' when using the line:

header('HTTP/1.0 401 Unauthorized');

Use instead: Header('status: 401 Unauthorized');

And everything will start working without changing the default Apache set-up.

Phillip.



 About Notes


Previous page
 HTTP
 Updated
Sat, 12 Aug 2000
header_sent 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.