Home >php教程 >php手册 >Detailed explanation of the usage and precautions of header function in PHP, detailed explanation of phpheader function

Detailed explanation of the usage and precautions of header function in PHP, detailed explanation of phpheader function

WBOY
WBOYOriginal
2016-07-06 14:24:35921browse

Detailed explanation of the usage and precautions of header function in PHP, detailed explanation of phpheader function

void header ( string $string [, bool $replace = true [, int $http_response_code ]] ) : Send a raw HTTP header

Here are some ways to use headers:

1. Use the header function to jump to the page;

header('Location:'.$url);

Where $url is the URL to be redirected.

 The following points should be noted when using this method:

•There cannot be a space between Location and ":", otherwise an error will occur (Note: I just tested it. In my local environment, there is no page jump, but no error is reported. I don't know the reason);

•There cannot be any output before using the header (note: everyone knows this, if there is any output before the header, including blanks, the error header already sent by xxx will appear);

•What follows the header will still be executed;

2. Use header to declare content-type

header('content-type:text/html;charset=utf-8');
There’s nothing to say about this;

3. Use header to return response status code

 header(sprintf('%s %d %s', $http_version, $status_code, $description));

The style is like this;

For example: header('HTTP/1.1 404 Not Found');

4. Use header to perform jump after a certain time

header("Refresh: {$delay}; url={$url}");

Where $delay is the time to delay the jump, $url is the url that needs to be jumped

For example: header('Refresh: 10; url=http://www.example.org/'); means to jump to the website http://www.eexample.org after 10 seconds

5. Use header to control browser 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");

6. Perform http verification

header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="Top Secret"');

7. Use header to download

header('Content-Type: application/octet-stream');//设置内容类型
  header('Content-Disposition: attachment; filename="example.zip"'); //设置MIME用户作为附件下载 如果将attachment换成inline意思为在线打开
  header('Content-Transfer-Encoding: binary');//设置传输方式
  header('Content-Length: '.filesize('example.zip'));//设置内容长度
  // load the file to send:
  readfile('example.zip');//读取需要下载的文件

The following will introduce you to several uses of PHP header

Jump page

header('Location:'.$url); //No space between Location and ":".

Declare content-type

header('content-type:text/html;charset=utf-8');

Return response status code

header('HTTP/1.1 404 Not Found');

Execute jump after a certain time

header('Refresh: 10; url=http://www.baidu.com/'); //Jump after 10s.

Control browser 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");

Perform http verification

header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="Top Secret"');

Perform download operation

header('Content-Type: application/octet-stream'); //Set the content type
header('Content-Disposition: attachment; filename="example.zip"'); //Set MIME user as attachment
header('Content-Transfer-Encoding: binary'); //Set the transmission method
header('Content-Length: '.filesize('example.zip')); //Set content length

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn