PHP header函数分析详解

yipeiwu_com5年前PHP代码库
在php语言中,header()这个函数很有用的,尤其在用到ajax时候,他会帮你解决一些意想不到的问题。下面是header的一些详细讲解。希望对phper有帮助
复制代码 代码如下:

<?php
// fix 404 pages:
header('HTTP/1.1 200 OK');
// set 404 header:
header('HTTP/1.1 404 Not Found');
// set Moved Permanently header (good for redrictions)
// use with location header
header('HTTP/1.1 301 Moved Permanently');
// redirect to a new location:
header('Location: http://www.example.org/');
// redrict with delay:
header('Refresh: 10; url=http://www.example.org/');
print 'You will be redirected in 10 seconds';
// you could also use the HTML syntax:// <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');
// content language (en = English)
header('Content-language: en');
// last modified (good for caching)
$time = time() – 60; // or filemtime($fn), etc
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');
// header for telling the browser that the content
// did not get changed
header('HTTP/1.1 304 Not Modified');
// set content length (good for caching):
header('Content-Length: 1234');
// Headers for an download:
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');
// Disable caching of the current document:
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 pastheader('Pragma: no-cache');
// set content type:
header('Content-Type: text/html; charset=iso-8859-1');
header('Content-Type: text/html; charset=utf-8');
header('Content-Type: text/plain');
// plain text file
header('Content-Type: image/jpeg');
// JPG picture
header('Content-Type: application/zip');
// ZIP file
header('Content-Type: application/pdf');
// PDF file
header('Content-Type: audio/mpeg');
// Audio MPEG (MP3,…) file
header('Content-Type: application/x-shockwave-flash');
// Flash animation// show sign in box
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设计模式 Prototype (原型模式)代码

复制代码 代码如下:<?php /** * 原型模式 * * 用原型实例指定创建对象的种类.并且通过拷贝这个原型来创建新的对象 * */ abstract class Protot...

PHP数据缓存技术

数据缓存是web开发中常用的一种性能优化方法。目前主要文件缓存或者数据库缓存两种形式,数据库缓存数据库不是什么不可能的事情,的确也是很好很重要的。我认为传统数据库主要是从业务层、模块设计...

php清空(删除)指定目录下的文件,不删除目录文件夹的实现代码

web 开发中我们可能会碰到需要将某个目录下的所有文件清空,但不删除这个目录下的子目录(当然也不会删除被删除的根目录)的情况。那么对于这种只删除文件而不删除目录的方法,你是如何处理的呢?...

PHP实现导出带样式的Excel

工作中做导出的时候,需要导出自定义的表格或嫌弃导出的Excel格式太难看了。 需要设置颜色、字号大小、加粗、合并单元格等等。 效果图: PHP代码: /** * 导出文件 * @r...

php中在PDO中使用事务(Transaction)

并且在执行的过程中, 如果其中的某条执行失败, 可以回滚所有已更改的操作. 如果执行成功, 那么这一系列操作都会永久有效. 事务很好的解决了在操作数据库的时候不同步的问题. 同时, 通过...