header ( string $header , bool $replace = true , int $response_code = 0 ) : void

Bạn đang xem: Php header homepage

header() is used khổng lồ 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 It is a very comtháng error khổng lồ read code with include , or require , functions, or another tệp tin access function, & have sầu spaces or empty lines that are output before header() is called. The same problem exists when using a single tệp tin.

/* This will give an error. Note the output
 * above sầu, which is before the header() call */


header The header string.

There are two special-case header calls. The first is a header that starts with the string "HTTP/" (case is not significant), which will be used khổng lồ figure out the HTTP status code lớn skết thúc. For example, if you have sầu configured Apabít khổng lồ use a obatambeienwasirherbal.comhường. script to handle requests for missing files (using the ErrorDocument directive), you may want khổng lồ make sure that your script generates the proper status code.

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

The second special case is the "Location:" header. Not only does it skết thúc this header bachồng khổng lồ the browser, but it also returns a REDIRECT (302) status code to the browser unless the 201 or a 3xx status code has already been set.

header("Location:"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */


The optional replace parameter indicates whether the header should replace a previous similar header, or add a second header of the same type. By default it will replace, but if you pass in false as the second argument you can force multiple headers of the same type. For example:

header("WWW-Authenticate: Negotiate");
header("WWW-Authenticate: NTLM"false);


Forces the HTTP response code to lớn the specified value. chú ý that this parameter only has an effect if the header is not empty.

Return Values

No value is returned.


Example #1 Download dialog

If you want the user khổng lồ be prompted khổng lồ save the data you are sending, such as a generated PDF tệp tin, you can use the » Content-Disposition header khổng lồ supply a recommended filename và force the browser to display the save sầu dialog.

// 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

Xem thêm: push item array php

Example #2 Caching directives scripts often generate dynamic content that must not be cached by the client browser or any proxy caches between the hệ thống and the client browser. Many proxies & clients can be forced to disable caching with:

header("Cache-Control: no-cabịt, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past


You may find that your pages aren't cached even if you don't output all of the headers above sầu. There are a number of options that users may be able khổng lồ set for their browser that change its mặc định caching behavior. By sending the headers above, you should override any settings that may otherwise cause the output of your script khổng lồ be cached.

Additionally, session_cache_limiter() & the session.cache_limiter configuration setting can be used khổng lồ automatically generate the correct caching-related headers when sessions are being used.



Headers will only be accessible và output when a SAPI that supports them is in use.


You can use output buffering khổng lồ get around this problem, with the overhead of all of your output to lớn the browser being buffered in the server until you send it. You can bởi vì this by calling ob_start() and ob_end_flush() in your script, or setting the output_buffering configuration directive sầu on in your or server configuration files.


The HTTP.. status header line will always be the first sent to lớn the client, regardless of the actual header() Gọi 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 sầu already been sent.


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 lớn Service Pachồng 2 or later.


Most contemporary clients accept relative URIs as argument to » Location:, but some older clients require an absolute URI including the scheme, hostname và absolute path. You can usually use $_SERVER<"HTTP_HOST">, $_SERVER<"obatambeienwasirherbal.com_SELF"> & dirname() khổng lồ make an absolute URI from a relative sầu one yourself:
/* Redirect to a different page in the current directory that was requested */
$host  $_SERVER<"HTTP_HOST">;
$uri   rtrim(dirname($_SERVER<"obatambeienwasirherbal.com_SELF">), "/\");
$extra "";
header("Location: http://$host$uri/$extra");


Session ID is not passed with Location header even if session.use_trans_sid is enabled. It must by passed manually using SID constant.

See Also

headers_sent() - Checks if or where headers have been sent setcookie() - Send a cookie http_response_code() - Get or Set the HTTP.. response code header_remove() - Remove sầu previously phối headers The section on HTTPhường authentication

Convert a string to a number in php
  • Convert datetime to string in php
  • Php convert now to timestamp
  • Check character in string php