php教程

PHP常用header()函数代码大全

我的站长站 2020-09-28 人阅读

在php的开发中,我们常常需要使用到header函数头来进行做标记

header() 函数向客户端发送原始的 HTTP 报头。

常用header设置列表如下:

header('HTTP/1.1 200 OK'); // ok 正常访问
header('HTTP/1.1 404 Not Found'); //通知浏览器 页面不存在
header('HTTP/1.1 301 Moved Permanently'); //设置地址被永久的重定向 301
header('Location: https://www.wdzzz.com/'); //跳转到一个新的地址
header('Refresh: 10; url=https://www.wdzzz.com/'); //延迟转向 也就是隔几秒跳转
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/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('Content-Disposition: attachment; filename="ITblog.zip"');
header('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"');

声明一个需要下载的xls文件

header('Content-Disposition: attachment; filename=ithhc.xlsx');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Length: '.filesize('./test.xls'));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
readfile('./test.xls');


相关推荐
  • PHP函数
  • php代码
  • header头
  • PHP数组转换为JSON格式数据

    PHP数组转换为JSON格式数据,这里介绍下PHP json_encode函数,他可以轻松完成转换。json_encode函数PHP json_encode() 用于对变量进行 JSON 编码,该函数如果执行成功返回 JSON 数据,否则返回 FALSE 。json_encode语法string json_encode ( $value [, $opt...

    php教程 98 3年前
  • 利用get_headers函数判断链接是否有效或失效

    PHP判断链接是否有效或失效的方法代码,get_headers() 是PHP系统级函数,他返回一个包含有服务器响应一个 HTTP 请求所发送的标头的数组。如果失败则返回 FALSE 并发出一条 E_WARNING 级别的错误信息(可用来判断远程文件是否存在)。函数定义array get_he...

    php教程 117 3年前
  • 利用explode() 函数分隔字符

    explode()函数本函数为 implode() 的反函数,使用一个字符串分割另一个字符串,返回一个数组。语法:codearray explode( string separator, string string [, int limit] )参数说明:separator 分割标志 string 需要分割的字符串 limit 可选...

    php教程 75 3年前
  • 支付宝企业账户转账个人账户php接口代码

    前言支付宝企业账户转账个人账户php接口代码,该接口主要用于平台对用户奖励的发放,场景也很简单就是我在你平台上有余额,我提现,平台给钱。只不过项目中是用的官方的SDK调用的,仔细看了下那个SDK目录,把很多无用的接口(目前自己用不上)也包含在里面,作为强...

    php教程 161 2年前
  • php判断目录文件是否存在

    一段简单的php判断目录文件是否存在代码,收藏记录一下,以后肯定用得到的。<?php$filename = &#39;../../e/install/&#39;; if (file_exists($filename)) { echo "<h3><div align=&#39;center&#39;><font color=&#39;#FF0000&#39;>存在安全风险!请将 ...

    php教程 115 2年前
  • curl函数获取API接口数据方法

    PHP利用curl函数,获取API接口数据方法示例代码,自用收藏<?php $weather = curl_init(); curl_setopt($weather,CURLOPT_URL,"https://api.pc2801.com/cqssc/".time()); curl_setopt($weather, CURLOPT_SSL_VERIFYPEER, false); //如果...

    php教程 89 2年前
  • nginx获取header头信息教程

    在PHP中getallheaders可以直接获取到header头信息,但getallheaders在nginx环境中无法使用,所以在nginx就需要用其他的方法了。分享一个我的站长站自己的编写的函数,代码如下:function NginxGetAllHeaders(){//获取请求头 $headers = []; foreach (...

    php教程 71 1年前
  • 利用get_headers函数判断链接是否有效或失效

    PHP判断链接是否有效或失效的方法代码,get_headers() 是PHP系统级函数,他返回一个包含有服务器响应一个 HTTP 请求所发送的标头的数组。如果失败则返回 FALSE 并发出一条 E_WARNING 级别的错误信息(可用来判断远程文件是否存在)。函数定义array get_he...

    php教程 117 3年前
  • 帝国CMS报错Warning: Cannot modify header information

    今天偶然进入帝国CMS网站后台发现出现了:Warning:Cannot modify header information - headers already sent by (output started at /htdocs/e/config/config.php:1) in /htdocs/e/class/connect.php on line 189 --phpfensi.com 错误。因为这个网站...

    帝国cms教程 42 1年前
最新更新