PHP header()函数常用方法总结

yipeiwu_com5年前PHP代码库
//定义编码
复制代码 代码如下:
header( 'Content-Type:text/html;charset=utf-8 ');

//Atom
复制代码 代码如下:
header('Content-type: application/atom+xml');

//CSS
复制代码 代码如下:
header('Content-type: text/css');

//Javascript
复制代码 代码如下:
header('Content-type: text/javascript');

//JPEG Image
复制代码 代码如下:
header('Content-type: image/jpeg');

//JSON
复制代码 代码如下:
header('Content-type: application/json');

//PDF
复制代码 代码如下:
header('Content-type: application/pdf');

//RSS
复制代码 代码如下:
header('Content-Type: application/rss+xml; charset=ISO-8859-1');

//Text (Plain)
复制代码 代码如下:
header('Content-type: text/plain');

//XML
复制代码 代码如下:
header('Content-type: text/xml');

// ok
复制代码 代码如下:
header('HTTP/1.1 200 OK');

//设置一个404头:
复制代码 代码如下:
header('HTTP/1.1 404 Not Found');

//设置地址被永久的重定向
复制代码 代码如下:
header('HTTP/1.1 301 Moved Permanently');

//转到一个新地址
复制代码 代码如下:
header('Location: http://www.example.org/');

//文件延迟转向:
复制代码 代码如下:
header('Refresh: 10; url=http://www.example.org/');
print 'You will be redirected in 10 seconds';
//当然,也可以使用html语法实现
// <meta http-equiv="refresh" content="10;http://www.example.org/ />

// override X-Powered-By: PHP:
复制代码 代码如下:
header('X-Powered-By: PHP/4.4.0');
header('X-Powered-By: Brain/0.6b');

//文档语言
复制代码 代码如下:
header('Content-language: en');

//告诉浏览器最后一次修改时间
复制代码 代码如下:
$time = time() - 60; // or filemtime($fn), etc
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');

//告诉浏览器文档内容没有发生改变
复制代码 代码如下:
header('HTTP/1.1 304 Not Modified');

//设置内容长度
复制代码 代码如下:
header('Content-Length: 1234');

//设置为一个下载类型
复制代码 代码如下:
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="example.zip"');
header('Content-Transfer-Encoding: binary');
// load the file to send:
readfile('example.zip');

// 对当前文档禁用缓存
复制代码 代码如下:
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Pragma: no-cache');

//设置内容类型:
复制代码 代码如下:
header('Content-Type: text/html; charset=iso-8859-1');
header('Content-Type: text/html; charset=utf-8');
header('Content-Type: text/plain'); //纯文本格式
header('Content-Type: image/jpeg'); //JPG***
header('Content-Type: application/zip'); // ZIP文件
header('Content-Type: application/pdf'); // PDF文件
header('Content-Type: audio/mpeg'); // 音频文件
header('Content-Type: application/x-shockw**e-flash'); //Flash动画

//显示登陆对话框
复制代码 代码如下:
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="Top Secret"');
print 'Text that will be displayed if the user hits cancel or ';
print 'enters wrong login data';

相关文章

PHP中对数组的一些常用的增、删、插操作函数总结

有时候我们需要扩展一个数组,或者删掉数组的一部分,PHP为扩展和缩小数组提供了一些函数。对于那些希望模仿各种队列实现(FIFO、LIFO)的程序员来说,这些函数可以提供便利。顾名思义,从...

自己写的兼容低于PHP 5.5版本的array_column()函数

array_column 用于获取二维数组中的元素(PHP 5.5新增函数),但我们有时候需要在低版本的PHP环境中使用… if( ! function_exists('array_...

提高PHP性能的编码技巧以及性能优化详细解析

0、用单引号代替双引号来包含字符串,这样做会更快一些。因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这 么做,它是 一种可以把多个字符串当作参数的“函数”...

PHP数组函数array_multisort()用法实例分析

本文实例分析了PHP数组函数array_multisort()用法。分享给大家供大家参考,具体如下: 有时候我们需要对二维数组的某个键的值进行排序,这里就是讨论这个问题。我们可以使用ar...

FCKeditor添加自定义按钮

在FCKeditor目录里的fckconfig.js打开,找到FCKConfig.ToolbarSets["Default"] 这里的设置是配置功能按钮的,你需要的留下,不需要的可以删掉...