Home > Backend Development > PHP Problem > What does php header mean?

What does php header mean?

藏色散人
Release: 2023-03-08 10:24:01
Original
3583 people have browsed it

Header means header, which is a built-in http function in php, used to send the original HTTP header to the client. Its usage syntax is "header(string,replace,http_response_code)"; the parameter string Specifies the header string to be sent. Headers are commonly used to notify the browser that the page does not exist, delay redirection, indicate content type, declare downloaded files, disable caching of the current document, display a login dialog box that requires verification, etc.

What does php header mean?

The operating environment of this article: windows7 system, PHP8 version, DELL G3 computer

header means header.

php header() function sends the original HTTP header to the client. It is often used to notify the browser that the page does not exist, delay redirection, indicate content type, declare downloaded files, and Disable caching of the current document, display a login dialog requiring authentication, etc.

The header function is commonly declared in settings:

header('HTTP/1.1 200 OK'); // ok 正常访问
 
header('HTTP/1.1 404 Not Found'); //通知浏览器 页面不存在
 
header('HTTP/1.1 301 Moved Permanently'); //设置地址被永久的重定向 301
 
header('Location: http://www.ithhc.cn/'); //跳转到一个新的地址
 
header('Refresh: 10; url=http://www.ithhc.cn/'); //延迟转向 也就是隔几秒跳转
 
header('X-Powered-By: PHP/6.0.0'); //修改 X-Powered-By信息
 
header('Content-language: en'); //文档语言
 
header('Content-Length: 1234'); //设置内容长度
 
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT'); //告诉浏览器最后一次修改时间
 
header('HTTP/1.1 304 Not Modified'); //告诉浏览器文档内容没有发生改变
Copy after login

It is important to realize that the header() function must be called before any actual output is sent (in PHP 4 and higher version, you can use output caching to solve this problem):

<html>
<?php
// 结果出错
// 在调用 header() 之前已存在输出
header(&#39;Location: http://www.example.com/&#39;);
?>
Copy after login

Syntax

header(string,replace,http_response_code)
Copy after login

Parameters

  • string Required. Specifies the header string to be sent.

  • replace

    Optional. Indicates whether this header replaces the previous header, or adds a second header.

    The default is true (replacement). false (allow multiple headers of the same type).

  • http_response_code Optional. Forces the HTTP response code to the specified value. (Available in PHP 4 and above)

Note: From PHP 4.4 onwards, this function prevents multiple headers from being sent at once. This is a protection measure against header injection attacks.

Example

Example 1

<?php
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
?>
<html>
<body>
...
...
Copy after login

Note: The user may set some options to change the browser's default cache settings. By sending the header above, you can override any of these settings and force the browser not to cache!

Example 2

Prompts the user to save a generated PDF file (the Content-Disposition header is used to provide a recommended file name and force the browser to display a save dialog):

<?php
header("Content-type:application/pdf");
// 文件将被称为 downloaded.pdf
header("Content-Disposition:attachment;filename=&#39;downloaded.pdf&#39;");
// PDF 源在 original.pdf 中
readfile("original.pdf");
?>
<html>
<body>
...
...
Copy after login

Note: Microsoft IE 5.5 has a bug that prevents the above mechanism. This bug can be resolved by upgrading to Service Pack 2 or later.

[Recommended learning: "PHP Video Tutorial"]

The above is the detailed content of What does php header mean?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template