search
Homephp教程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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.