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

How to use the header() function in php

小云云
Release: 2023-03-22 15:40:01
Original
2348 people have browsed it

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后跳转。
Copy after login

5. Control browser cache

NameMeaningExample
ExpiresThe date and time the response expiresExpires: Thu, 01 Dec 2010 16 :00:00 GMT
Last-ModifiedThe last modified time of the requested resourceLast-Modified: Tue, 15 Nov 2010 12:45:26 GMT
Cache-Control Tells all caching mechanisms whether they can be cached and what typeCache-Control: no-cache
Pragma#Contains implementation-specific directives that can be applied to the response chain Any receiverPragma: 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");
Copy after login

#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"');
Copy after login
NameMeaningExample
##WWW-Authenticate
7. Perform download operation

NameMeaningExample##Content-Disposition
header('Content-Type: application/octet-stream'); //设置内容类型header('Content-Disposition: attachment; filename="example.zip"'); //设置MIME用户
Copy after login
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')); //设置内容长度
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template