PHP header()函数的使用
在之前文章中,介绍了 PHP 中 header() 函数的一些情况,这里再补充一下吧。
下面是关于header函数的详细使用说明:
不管页面有多少 header,它会执行最后一个,不过是有条件的,例如:
header('Location:http://www.bkjia.com'); header('Location:http://www.g.cn'); header('Location:http://www.baidu.com');
这个就会跳到百度
header函数的作用
PHP只是以HTTP协议将HTML文档的标头送到浏览器,告诉浏览器具体怎么处理这个页面,至于传送的内容则需要熟悉一下HTTP协议了,与PHP无关了。
传统的标头一定包含下面三种标头之一,并只能出现一次。
Location: xxxx:yyyy/zzzz Content-Type: xxxx/yyyy Status: nnn xxxxxx
HTTP协议的运作方式
HTTP协议是基于请求/响应范式的。一个客户机与服务器建立连接后,发送一个请求给服务器,请求方式的格式为,统一资源标识符、协议版本号,后边是 MIME信息包括请求修饰符、客户机信息和可能的内容。服务器接到请求后,给予相应的响应信息,其格式为一个状态行包括信息的协议版本号、一个成功或错误的代码,后边是MIME信息包括服务器信息、实体信息和可能的内容。
它分四个过程,在HTTP协议中,服务端是指提供HTTP服务的部分,客户端是指你使用的浏览器或者下载工具等等。在通讯时,由客户端发出请求连接,服务端建立连接;然后,客户端发出HTTP请求(Request),服务端返回响应信息(Respond),由此完成一个HTTP操作。
HTTP协议状态码表示的意思
1×× 保留 2×× 表示请求成功地接收 3×× 为完成请求客户需进一步细化请求 4×× 客户错误 5×× 服务器错误
操作实例
- 重定向功能,这种最常见。
- 强制用户每次访问这个页面时获取最新资料,而不是使用存在客户端的缓存。
- 输出状态值到浏览器,主要用于访问权限控制。
<?php Header("Location: http://www.php.net/"); ?>
<?php //告诉浏览器此页面的过期时间(用格林威治时间表示),只要是已经过去的日期即可。 header("Expires: Mon, 26 Jul 1970 05:00:00 GMT"); //告诉浏览器此页面的最后更新日期(用格林威治时间表示)也就是当天,目的就是强迫浏览器获取最新资料 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT"); //告诉客户端浏览器不使用缓存 header("Cache-Control: no-cache, must-revalidate"); //参数(与以前的服务器兼容),即兼容HTTP1.0协议 header("Pragma: no-cache"); //输出MIME类型 header("Content-type: application/file"); //文件长度 header("Content-Length: 227685"); //接受的范围单位 header("Accept-Ranges: bytes"); //缺省时文件保存对话框中的文件名称 header("Content-Disposition: attachment; filename=$filename"); ?>
<?php header('HTTP/1.1 401 Unauthorized'); header('status: 401 Unauthorized'); ?>
比如要限制一个用户不能访问该页,则可设置状态为404,如下所示,这样浏览器就显示为即该页不存在 。
<?php header('HTTP/1.1 404 Not Found'); header("status: 404 Not Found"); ?>
注意: 传统的标头一定包含下面三种标头之一,并只能出现一次。 Content-Type: xxxx/yyyy Location: xxxx:yyyy/zzzz Status: nnn xxxxxx 在新的多型标头规格 (Multipart MIME) 方可以出现二次以上。
使用范例
- 范例一: 本例使浏览器重定向到 PHP 的官方网站。
- 范例二: 要使用者每次都能得到最新的资料,而不是 Proxy 或 cache 中的资料,可以使用下列的标头:
- 范例三: 让使用者的浏览器出现找不到档案的信息。
- 范例四:让使用者下载档案。
Header("Location: http://www.php.net/"); exit;
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");
header("Status: 404 Not Found");
header("Content-type: application/x-gzip"); header("Content-Disposition: attachment; filename=文件名"); header("Content-Description: PHP3 Generated Data");

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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

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

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

In this chapter, we are going to learn the following topics related to routing ?

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

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

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