Home Backend Development PHP Problem What does php header mean?

What does php header mean?

Feb 28, 2021 am 09:06 AM
php

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!

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles