跳转到主内容
极星编程网:以代码为星,赴技术山海!

PHP中的header函数怎么用?

大家好,我是苏承栈。今天我们来聊聊PHP中那个神奇的header函数,它可是控制HTTP响应头的关键角色哦!

header函数的作用

header函数主要用于向客户端发送原始的HTTP报头,比如通知浏览器页面不存在、延迟转向、设置内容类型、声明下载文件、禁用缓存、显示登录对话框等等。听起来是不是很强大?接下来我们就来详细看看它的用法。

header函数的常见设置

header('HTTP/1.1 200 OK'); // 正常访问
header('HTTP/1.1 404 Not Found'); // 通知浏览器页面不存在
header('HTTP/1.1 301 Moved Permanently'); // 设置地址被永久重定向
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'); // 告诉浏览器文档内容没有发生改变

表示内容(文件)类型

header('Content-Type: text/html; charset=utf-8'); // 网页编码
header('Content-Type: text/plain'); // 纯文本格式
header('Content-Type: image/jpeg'); // JPG、JPEG
header('Content-Type: application/zip'); // ZIP文件
header('Content-Type: application/pdf'); // PDF文件
header('Content-Type: audio/mpeg'); // 音频文件
header('Content-type: text/css'); // CSS文件
header('Content-type: text/java'); // JS文件
header('Content-type: application/json'); // JSON
header('Content-type: application/pdf'); // PDF
header('Content-type: text/xml'); // XML
header('Content-Type: application/x-shockw**e-flash'); // Flash动画

声明一个下载的文件

header('Content-Type: application/octet-stream');
header(
                            

相关文章