Home  >  Article  >  Backend Development  >  How to use the header() function in php

How to use the header() function in php

小云云
小云云Original
2018-03-29 10:08:152307browse

This article mainly shares with you how to use the header() function in php. It mainly explains it to you in the form of code. I hope it can help you.

 跳转页面
header('Location:'.$url);  //Location和":"之间无空格。
2、 声明content-type
header('content-type:text/html;charset=utf-8');
3、 返回response状态码
header('HTTP/1.1 404 Not Found');
4、 在某个时间后执行跳转
header('Refresh: 10; url=http://www.baidu.com/');  //10s后跳转。

5. Control browser cache

Name Meaning Example
Expires The date and time the response expires Expires: Thu, 01 Dec 2010 16 :00:00 GMT
Last-Modified The last modified time of the requested resource Last-Modified: Tue, 15 Nov 2010 12:45:26 GMT
Cache-Control Tells all caching mechanisms whether they can be cached and what type Cache-Control: no-cache
Pragma #Contains implementation-specific directives that can be applied to the response chain Any receiver Pragma: no-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

Indicates the authorization scheme that the client requesting entity should useWWW-Authenticate: Basic
header('HTTP/1.1 401 Unauthorized');header('WWW-Authenticate: Basic realm="Top Secret"');
Name Meaning Example
##WWW-Authenticate
7. Perform download operation

NameMeaningExample##Content-Disposition
header('Content-Type: application/octet-stream'); //设置内容类型header('Content-Disposition: attachment; filename="example.zip"'); //设置MIME用户
The message header indicates in what form the reply content should be displayed, whether in inline form (that is, a web page or part of a page), or downloaded and saved as an attachment. to local. Content-Disposition: attachment; filename="filename.jpg"
8.As an attachment

header('Content-Transfer-Encoding: binary'); //设置传输方式header('Content-Length: '.filesize('example.zip')); //设置内容长度

Related recommendations:

php: Detailed explanation of header() function usage examples

How header() outputs image cache usage examples

10 recommended articles about php header() function

The above is the detailed content of How to use the header() function in php. For more information, please follow other related articles on the PHP Chinese website!

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