2019独角兽企业重金招聘Python工程师标准>>>
送出 HTTP 协议的标头到浏览器
语法: int header(string string);
返回值: 整数
函数种类: 网络系统
|
|
内容说明 |
标头 (header) 是服务器以 HTTP 协议传 HTML 资料到浏览器前所送出的字符串,在标头与 HTML 文件之间尚需空一行分隔。有关 HTTP 的详细说明,可以参考坊间的相关书籍或更详细的 RFC 2068 官方文件(http://www.w3.org/Protocols/rfc2068/rfc2068)。在 PHP 中送回 HTML 资料前,需先传完所有的标头。
注意: 传统的标头一定包含下面三种标头之一,并只能出现一次。
- Content-Type: xxxx/yyyy
- Location: xxxx:yyyy/zzzz
- Status: nnn xxxxxx
在新的多型标头规格 (Multipart MIME) 方可以出现二次以上。
|
|
使用范例 |
范例一: 本例用来重导用户到 PHP 的官方网站。
Header("Location: http://www.php.net");
exit;
?>
范例二: 欲让用户每次都能得到最新的资料,而不是 Proxy 或 cache 中的资料,可以使用下列的标头
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
范例三: 让用户的浏览器出现找不到文件的信息。
header("Status: 404 Not Found");
?>
范例四: [email protected] (28-Apr-1999) 提供让用户下载文件的范例。
header("Content-type: application/x-gzip");
header("Content-Disposition: attachment; filename=some-file.tar.gz");
header("Content-Description: PHP3 Generated Data");
(PHP 4, PHP 5)
header — Send a raw HTTP header
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.
/* This will give an error. Note the output
* above, which is before the header() call */
header('Location: http://www.example.com/');
?>
string
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 to figure out the HTTP status code to send. For example, if you have configured Apache to use a PHP script to handle requests for missing files (using the ErrorDocument directive), you may want to make sure that your script generates the proper status code.
?>
The second special case is the "Location:" header. Not only does it send this header back to 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.
/* Make sure that code below does not get executed when we redirect. */
exit;
?>
replace
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: NTLM', false);
?>
http_response_code
Forces the HTTP response code to the specified value.
No value is returned.
Version Description
4.4.2 and 5.1.2 | This function now prevents more than one header to be sent at once as a protection against header injection attacks. |
4.3.0 | The http_response_code parameter was added. |
4.0.4 | The replace parameter was added. |
Example #1 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.
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 #2 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:
header("Expires: Sat, 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.
Note: 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 output_buffering configuration directive on in your php.ini or server configuration files.
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 realm part of the WWW-Authenticate header if you set this header (used for HTTP Authentication).
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 $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself:
/* Redirect to a different page in the current directory that was requested */
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '///');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;
?>
Note: Session ID is not passed with Location header even if session.use_trans_sid is enabled. It must by passed manually using SID constant.
- headers_sent() - Checks if or where headers have been sent
- setcookie() - Send a cookie
- The section on HTTP authentication
header
11-Dec-2009 07:13
headers_sent(&$file, &$line);
var_dump($file, $line);
?>
This will show you exactly where the first line of output is coming from, which will prevent your headers from being set.
16-Nov-2009 06:31
Everything else is workaround.
I had download error with first attempt always till i had cache-control header within script using Internet Explorer.
Such code works fine with IE 7-8:
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT/n");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Content-type: application/zip;/n"); //or yours?
header("Content-Transfer-Encoding: binary");
$len = filesize($filename);
header("Content-Length: $len;/n");
$outname="downfile.zip";
header("Content-Disposition: attachment; filename=/"$outname/";/n/n");
readfile($filename);
?>
17-Oct-2009 05:45
Content-Transfer-Encoding specifies the encoding used to transfer the data within the HTTP protocol, like raw binary or base64. (binary is more compact than base64. base64 having 33% overhead).
Eg Use:- header('Content-Transfer-Encoding: binary');
Content-Encoding is used to apply things like gzip compression to the content/data.
Eg Use:- header('Content-Encoding: gzip');
16-Oct-2009 11:42
PHP, by default, always returns the following header:
"Content-Type: text/html"
Which your entire header response will look like
HTTP/1.1 200 OK
Server: Apache/2.2.11 (Unix)
X-Powered-By: PHP/5.2.8
Date: Fri, 16 Oct 2009 23:05:07 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
If you call the header name with no value like so...
header( 'Content-Type:' );
?>
Your headers now look like this:
HTTP/1.1 200 OK
Server: Apache/2.2.11 (Unix)
X-Powered-By: PHP/5.2.8
Date: Fri, 16 Oct 2009 23:05:07 GMT
Connection: close
05-Oct-2009 12:21
Thanks to the notification from guy.paddock AT gmail DOT com, I have accordingly changed the example inside my little page, in a way to respect the types of the parameters.
http://gif.phpnet.org/frederic/programs/http_status_codes/
I can repeat here an example:
('x', TRUE, 100); /* server will ouput: "HTTP/1.1 100 Continue" */ ?>
To be strict (actually PHP is rather tolerant) let us say:
- the first parameter has to be any NOT EMPTY string,
- the second parameter has to be a boolean, obviously it has to be TRUE, as it would be a nonsense to send several different status (anyway the server would not let you to do stupid things),
- the third parameter has to be an integer, between 100 and 599; an integer out of that range - or in range but of unknown value - will generate a
500 Internal Server Error
response.
I hope that helps ;)
06-Sep-2009 07:36
// seconds, minutes, hours, days
$expires = 60*60*24*14;
header("Pragma: public");
header("Cache-Control: maxage=".$expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
?>
31-Jul-2009 03:02
1. All used headers have first letters uppercase, so you MUST follow this. For example:
Location, not location
Content-Type, not content-type or CONTENT-TYPE
2. Then there MUST be colon and space, like
good: header("Content-Type: text/plain");
wrong: header("Content-Type:text/plain");
3. Location header MUST be absolute uri with scheme, port and so on.
good: header("Location: http://www.example.com/something.php?a=1");
4. It cann't be relative:
wrong: Location: /something.php?a=1
wrong: Location: ?a=1
It will make proxy server and http clients happier.
18-Jul-2009 06:01
I strongly recommend, that you use
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
instead of
header("HTTP/1.1 404 Not Found");
I had big troubles with an Apache/2.0.59 (Unix) answering in HTTP/1.0 while I (accidentially) added a "HTTP/1.1 200 Ok" - Header.
Most of the pages were displayed correct, but on some of them apache added weird content to it:
A 4-digits HexCode on top of the page (before any output of my php script), seems to be some kind of checksum, because it changes from page to page and browser to browser. (same code for same page and browser)
"0" at the bottom of the page (after the complete output of my php script)
It took me quite a while to find out about the wrong protocol in the HTTP-header.
10-Jun-2009 11:30
This script is enough long to process (using a socket loop connection) then it is executed actually once a day, but generates a cache file, to be served fast.
Note that here are the codes generated by PHP, not those which can be answered by Apache or any other server, nor any other 'exotic' status codes specific to some application: http://gif.phpnet.org/frederic/programs/http_status_codes/
03-Apr-2009 08:40
header("Status: 200");
header("Location: /home.php");
exit;
?>
08-Mar-2009 01:25
The encryption works very good but the speed is decrease by 2, it is now 520KiB/s. The user is now asked for a md5 password (instead of keeping it in the code directly). There is some part in French because it's my native language so modify it as you want.
// Stream files and encrypt the data on-the-fly
// Settings
// -- File to stream
$file = "FILE_out";
// -- Reading buffer
$bufferlength = 3840;
// -- Key in hex
//$keychar = "9cdfb439c7876e703e307864c9167a15";
// Function: Convertion hex key in a string into binary
function hex2bin($h) {
if (!is_string($h)) return null;
$r = array();
for ($a=; ($a*2)<strlen($h); $a++) {
$ta = hexdec($h[2*$a]);
$tb = hexdec($h[(2*$a+1)]);
$r[$a] = (int) (($ta << 4) + $tb);
}
return $r;
}
// Function to send the auth headers
function askPassword($text="Enter the password") {
header('WWW-Authenticate: Basic realm="'. utf8_decode($text) .'"');
header('HTTP/1.0 401 Unauthorized');
return 1;
}
// Key is asked at the first start
if (!isset($_SERVER['PHP_AUTH_PW'])) {
askPassword();
echo "Une clé est nécessaire !
";
exit;
}
// Get the key in hex
$keychar = $_SERVER['PHP_AUTH_PW'];
// Convert key and set the size of the key
$key = hex2bin($keychar);
$keylength = count($key);
// Teste si la clé est valide en hex
if ($key == "" || $keylength <= 4) {
askPassword("Clé incorrecte !");
//echo "Clé incorrecte !
";
exit();
}
// Teste si la clé est de longueur d'une puissance de 2
if ( ($keylength%2) != ) {
askPassword("Clé de longueur incorrecte (multiple de 2 uniquement)");
//echo "Clé de longueur incorrecte (puissance de 2 uniquement)
";
exit();
}
// Headers
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($file) ."; ");
header("filename=/"".$file."/"; ");
flush(); // this doesn't really matter.
// Opening the file in read-only
$fp = fopen($file, "r");
while (!feof($fp))
{
// Read a buffer size of the file
$buffer = fread($fp, $bufferlength);
$j=;
for ($i=; $i < $bufferlength; $i++) {
// The key is read in loop to crypt the whole file
if ($i%$keylength == ) {
$j=;
}
// Apply a xor operation between the key and the file to crypt
// This operation eats a lots of CPU time (Stream at 1MiB/s on my server; Intel E2180)
$tmp = pack("C", $key[$j]);
$bufferE = ( $buffer[$i]^$tmp); // <==== Le fameux XOR
/*
echo "
key[".$j."]: ";
var_dump($tmp);
echo "
buffer[".$i."]: ";
var_dump($buffer[$i]);
echo "
bufferE: ";
var_dump($bufferE);
echo "
";
//*/
// Send the encrypted data
echo $bufferE;
// Clean the memory
$bufferE = "";
$j++;
}
$buffer = "";
flush(); // this is essential for large downloads
/*
fclose($fp);
exit();
//*/
}
// Close the file and it's finished
fclose($fp);
?>
08-Mar-2009 06:17
To submit a form using POST method through PHP, just add the data to be posted as header. This essentially saves one extra html page sent to the browser when user has to be redirected. Mostly found this technique useful for redirecting to payment gateways.
$host = "www.example.com";
$path = "/path/to/script.php";
$data = "data1=value1&data2=value2";
$data = urlencode($data);
header("POST $path HTTP/1.1/r/n" );
header("Host: $host/r/n" );
header("Content-type: application/x-www-form-urlencoded/r/n" );
header("Content-length: " . strlen($data) . "/r/n" );
header("Connection: close/r/n/r/n" );
header($data);
?>
11-Feb-2009 08:05
In order to make Internet Explorer 6 (probably also 7) not to cache pages you should use only these headers:
header("Cache-Control: no-cache");
header("Expires: -1");
as suggested by Microsoft itself [source: http://support.microsoft.com/kb/234067]
if you add all the headers suggested in "Example #2 Caching directives" above:
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
IE browser goes on CACHING THE PAGE!!!
07-Feb-2009 12:15
My document root .htaccess was set up to redirect select error codes to error.php which tests against $_SERVER['REDIRECT_STATUS'] and templates a document based on the status code.
I had hoped to raise a 404 after a mysql query turned up empty (fyi; before output had started) but page output continued "normally", ie incorrectly.
Because this was inside a template class I was able to manually change the template's include file and $_SERVER['REDIRECT_STATUS'] to output the desired result. I wasn't actually sure if header() worked or not because apache2 has already returned 200 OK after which any header change would only affect the browser, wouldn't it?
I have read about header() working with apache redirects before, is this still available with apache2? Here's some code just to explain the concept:
class page_template
{
$include_file;
//...
function getData ()
{
try {
//...
if ($result->num_rows == ) {
throw new MysqlException("Empty result set");
}
//...
} catch (MysqlException $e) {
if ($e->getMessage() == "Empty result set") {
header("HTTP/1.0 404 Not Found");
header("Status: 404 Not Found");
//the below two lines fixed the issue for me
$_SERVER['REDIRECT_STATUS'] = 404;
$this->template_file = $_SERVER['DOCUMTENT_ROOT'].'error.php';
}
}
}
?>
07-Jan-2009 10:11
I have seen various examples where the people use a
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=/"".$filename."/";");
?>
however in adition to this is important add the definition of the cache because if you dont do it you will have problems with some navigators for example IE 7.0 under https protocol will show you a message saying that the file is not available.
here is a working example:
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header ( "Content-Type: $filedatatype" );
header("Content-Disposition: attachment; filename=/"".$FileObj->name."/";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$filesize);
readfile($file);
exit;
?>
where $filedatatype is the data type of the file for example:
application/pdf
03-Jan-2009 01:30
header("Location: ?mng=" . $_GET['mng']);
?>
but it didnt work in Safari so we are using this
header("Location: index.php?mng=" . $_GET['mng']);
?>
05-Dec-2008 01:46
So instead of typing:
header("Content-Disposition: attachment; filename=" . basename($filename));
?>
you should type:
header("Content-Disposition: attachment; filename=/"" . basename($filename) . "/"");
?>
If you don't do this then when the user clicks on the link for a file named "Example file with spaces.txt", then Firefox's Save As dialog box will give it the name "Example", and it will have no extension.
See the page called "Filenames_with_spaces_are_truncated_upon_download" at
http://kb.mozillazine.org/ for more information. (Sorry, the site won't let me post such a long link...)
01-Dec-2008 03:57
On another note: Safari can display CMYK images (at least the OS X version, because it uses the services of QuickTime)
23-Oct-2008 03:50
header("Content-Disposition: attachment; filename=" . urlencode($file));
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
header("Content-Length: " . filesize($file));
flush(); // this doesn't really matter.
$fp = fopen($file, "r");
while (!feof($fp))
{
echo fread($fp, 65536);
flush(); // this is essential for large downloads
}
fclose($fp);
?>
22-Oct-2008 10:39
$fp = fopen($_SERVER["SCRIPT_FILENAME"], "r");
$etag = md5(serialize(fstat($fp)));
fclose($fp);
header('Etag: '.$etag);
?>
22-Aug-2008 06:57
This redirects to 2.html since the second header replaces the first.
header("location: 1.html");
header("location: 2.html"); //replaces 1.html
?>
This redirects to 1.html since the header is sent as soon as the echo happens. You also won't see any "headers already sent" errors because the browser follows the redirect before it can display the error.
header("location: 1.html");
echo "send data";
header("location: 2.html"); //1.html already sent
?>
Wrapping the previous example in an output buffer actually changes the behavior of the script! This is because headers aren't sent until the output buffer is flushed.
ob_start();
header("location: 1.html");
echo "send data";
header("location: 2.html"); //replaces 1.html
ob_end_flush(); //now the headers are sent
?>
17-Aug-2008 06:41
$last_modified_time = filemtime($file);
$etag = md5_file($file);
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT");
header("Etag: $etag");
if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time ||
trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) {
header("HTTP/1.1 304 Not Modified");
exit;
}
?>
11-Jul-2008 01:35
ob_start();
?>
at the beginning of your page.
This starts the output buffer, which allows you to send headers whenever you feel like it. Make sure that you put it at the BEGINNING, after the first php tag.
It allows you to do something like
ob_start();
echo 'foo';
header("Status: 404 Not Found");
echo 'bar';
?>
08-Jul-2008 04:14
IE ignores custom error pages that are less than 512 (or from what i've read 1024) bytes.
just place this before any output on your custom error page--- and be sure that your custom error page includes proper html tags (it must have a for this specific example to work)
// set your custom error header --- example --- header('HTTP/1.1 503 Service Unavailable');
function padding($html){
return ($padding=1024-ob_get_length()) > 0 ? str_replace('