无名商城论坛

搜索
查看: 504|回复: 0

[其他技术] 关于PHP常用的header头部定义汇总代码

[复制链接]

1万

主题

1万

帖子

3万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
32464
发表于 2022-5-8 18:55:01 | 显示全部楼层 |阅读模式
一个关于PHP常用的header头部定义汇总代码,如果对你有帮助就看看吧。



代码:

?php
header('HTTP/1.1 200 OK'); // ok 正常访问
header('HTTP/1.1 404 Not Found'); //通知浏览器 页面不存在
header('HTTP/1.1.301 Moved Permanently'); //设置地址被永久的重定向 301
header('Location:
本帖隐藏的内容需要【资源会员】才可浏览,您需要升级才可浏览,点击这里升级【资源会员】
'); //跳转到一个新的地址
header('Refresh: 10; url=
本帖隐藏的内容需要【资源会员】才可浏览,您需要升级才可浏览,点击这里升级【资源会员】
'); //延迟转向 也就是隔几秒跳转
header('X-Powered-By: PHP/6.0.0'); //修改 X-Powered-By信息
header('Content-language: en'); //文档语言
header('Content-Length: 1.34'); //设置内容长度
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/javascript'); //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('Accept-Ranges:bytes');//声明浏览器返回大小是按字节进行计算
header('Content-Disposition: attachment; filename="ITblog.zip"');
//声明作为附件处理和下载后文件的名称//告诉浏览器文件的总大小
//告诉浏览器文件的总大小
    $fileSize = filesize($filePath);//坑 filesize 如果超过2G 低版本php会返回负数
    header('Content-Length:' . $fileSize); //注意是'Content-Length:' 非Accept-Lengthheader('Content-Transfer-Encoding: binary');
readfile('test.zip');
######
  
###对当前文档禁用缓存###
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
######
  
###显示一个需要验证的登陆对话框###
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="Top Secret"');
######
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表