2008年8月9日星期六

header 常用指令

header 分为三部分:

  • 第一部分为HTTP协议的版本(HTTP-Version)

  • 第二部分为状态代码(Status)

  • 第三部分为原因短语(Reason-Phrase)




fix 404 pages: 用这个 header 指令来解决 URL 重写产生的 404 header
[php]
header('HTTP/1.1 200 OK');
[/php]

set 404 header: 页面没找到
[php]
header('HTTP/1.1 404 Not Found');
[/php]

set Moved Permanently header (good for redrictions), use with location heade : 页面被永久删除,可以告诉搜索引擎更新它们的 urls
[php]
header('HTTP/1.1 301 Moved Permanently');
[/php]

访问受限
[php]
header('HTTP/1.1 403 Forbidden');
[/php]

服务器错误
[php]
header('HTTP/1.1 500 Internal Server Error');
[/php]

redirect to a new location: 重定向到一个新的位置
[php]
header('Location: http://www.google.com);
[/php]


redrict with delay: 延迟一段时间后重定向
[php]
header('Refresh: 10; url=http://www.sina.com.cn');
print 'You will be redirected in 10 seconds';
[/php]


override X-Powered-By: PHP: 覆盖 X-Powered-By value
[php]
header('X-Powered-By: PHP/4.4.0');
[/php]


content language (en = English): 内容语言 (en = English)
[php]
header('Content-language: en');
[/php]

last modified (good for caching): 最后修改时间 (在缓存的时候可以用到)
[php]
$time = time() - 60; // or filemtime($fn), etc
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');
[/php]

header for telling the browser that the content did not get changed: 告诉浏览器要获取的内容还没有更新
[php]
header('HTTP/1.1 304 Not Modified');
[/php]

set content length (good for caching): 设置内容的长度 (缓存的时候可以用到):
[php]
header('Content-Length: 1234');
[/php]

headers for an download: 用来下载文件
[php]
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="example.zip"');
header('Content-Transfer-Encoding: binary');
[/php]


disable caching of the current document: 禁止缓存当前文档
[php]
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
[/php]


设置内容类型
[php]
header('Content-Type: text/html; charset=utf-8');
[/php]


plain text file
[php]
header('Content-Type: text/plain');
[/php]

JPG picture
[php]
header('Content-Type: image/jpeg');
[/php]

ZIP file
[php]
header('Content-Type: application/zip');
[/php]

PDF file
[php]
header('Content-Type: application/pdf');
[/php]

Audio MPEG (MP3,...) file
[php]
header('Content-Type: audio/mpeg');
[/php]

Flash animation
[php]
header('Content-Type: application/x-shockwave-flash');
[/php]

显示登录对话框,可以用来进行 HTTP 认证
[php]
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 enters wrong login';
[/php]

表单填写时,可用 AJAX 对用户随时进行验证、提示,但是在用户没有留意 AJAX 提交了错误表单的提示信息时, ,跳回原页,而填写的信息全部丢失。要支持页面回跳,有以下的办法:

  • 使用 session_cache_limiter 方法: session_cache_limiter('private,must-revalidate');,但是要值得注意的是 session_cache_limiter() 方法要写在 session_start() 方法之前才有用。

  • 用header来设置控制缓存的方法: header('Cache-control:private,must-revalidate')

没有评论:

发表评论