欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP中Header使用的HTTP协议及常用方法小结

程序员文章站 2024-02-02 21:49:40
本文实例总结了php中header使用的http协议及常用方法。分享给大家供大家参考。具体方法如下: 复制代码 代码如下:

本文实例总结了php中header使用的http协议及常用方法。分享给大家供大家参考。具体方法如下:

复制代码 代码如下:
<?php
function https($num) {
$http = array (
100 => "http/1.1 100 continue",
101 => "http/1.1 101 switching protocols",
200 => "http/1.1 200 ok",
201 => "http/1.1 201 created",
202 => "http/1.1 202 accepted",
203 => "http/1.1 203 non-authoritative information",
204 => "http/1.1 204 no content",
205 => "http/1.1 205 reset content",
206 => "http/1.1 206 partial content",
300 => "http/1.1 300 multiple choices",
301 => "http/1.1 301 moved permanently",
302 => "http/1.1 302 found",
303 => "http/1.1 303 see other",
304 => "http/1.1 304 not modified",
305 => "http/1.1 305 use proxy",
307 => "http/1.1 307 temporary redirect",
400 => "http/1.1 400 bad request",
401 => "http/1.1 401 unauthorized",
402 => "http/1.1 402 payment required",
403 => "http/1.1 403 forbidden",
404 => "http/1.1 404 not found",
405 => "http/1.1 405 method not allowed",
406 => "http/1.1 406 not acceptable",
407 => "http/1.1 407 proxy authentication required",
408 => "http/1.1 408 request time-out",
409 => "http/1.1 409 conflict",
410 => "http/1.1 410 gone",
411 => "http/1.1 411 length required",
412 => "http/1.1 412 precondition failed",
413 => "http/1.1 413 request entity too large",
414 => "http/1.1 414 request-uri too large",
415 => "http/1.1 415 unsupported media type",
416 => "http/1.1 416 requested range not satisfiable",
417 => "http/1.1 417 expectation failed",
500 => "http/1.1 500 internal server error",
501 => "http/1.1 501 not implemented",
502 => "http/1.1 502 bad gateway",
503 => "http/1.1 503 service unavailable",
504 => "http/1.1 504 gateway time-out"
);
header($http[$num]);
};

//200 正常状态
header('http/1.1 200 ok');

// 301 永久重定向,记得在后面要加重定向地址 location:$url
header('http/1.1 301 moved permanently');

// 重定向,其实就是302 暂时重定向
header('location: http://www.xxxx.com/');

// 设置页面304 没有修改
header('http/1.1 304 not modified');

// 显示登录框,
header('http/1.1 401 unauthorized');
header('www-authenticate: basic realm="登录信息"');
echo '显示的信息!';

// 403 禁止访问
header('http/1.1 403 forbidden');

// 404 错误
header('http/1.1 404 not found');

// 500 服务器错误
header('http/1.1 500 internal server error');

// 3秒后重定向指定地址(也就是刷新到新页面与 <meta http-equiv="refresh" content="10;http://www.xxxx.com/ /> 相同)
header('refresh: 3; url=http://www.xxxx.com/');
echo '10后跳转到http://www.xxxx.com';

// 重写 x-powered-by 值
header('x-powered-by: php/5.3.0');
header('x-powered-by: brain/0.6b');

//设置上下文语言
header('content-language: en');

// 设置页面最后修改时间(多用于防缓存)
$time = time() - 60; //建议使用filetime函数来设置页面缓存时间
header('last-modified: '.gmdate('d, d m y h:i:s', $time).' gmt');

// 设置内容长度
header('content-length: 39344');

// 设置头文件类型,可以用于流文件或者文件下载
header('content-type: application/octet-stream');
header('content-disposition: attachment; filename="example.zip"');
header('content-transfer-encoding: binary');
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');
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');
header('content-type: application/zip');
header('content-type: application/pdf');
header('content-type: audio/mpeg');
header('content-type: application/x-shockwave-flash');
//.... 至于content-type 的值 可以去查查 w3c 的文档库,那里很丰富
?>

希望本文所述对大家的php程序设计有所帮助。