php header函数的作用与几种常用的代理整理
php中header()函数的作用是:发送一个原始 HTTP 标头[Http Header]到用户浏览器的客户端。
在 HTTP协议中,服务器端的回答(response)内容包括两部分:头信息(header)和体内容,这里的头信息不是HTML中的<head></head>部分,同样,体内容也不是HTML中的<body>< /body>标签部份。头信息是用户看不见的,里面包含了很多项,包括:服务器信息、日期、内容的长度等。而体内容就是整个HTML,也就是你所能看见的全部东西。
header函数用法
语法:
header(string,replace,http_response_code)
参数:
string:必需。规定要发送的报头字符串。
replace:可选。指示该报头是否替换之前的报头,或添加第二个报头。默认是 true(替换)。false(允许相同类型的多个报头)。
http_response_code:可选。把 HTTP 响应代码强制为指定的值。
代码:
<html>
<?php
// 结果出错
// 在调用 header() 之前已存在输出
header('Location: http://4dn.net/');
?>
header函数几种常用的代码整理
1. php跳转页面
header('Location:'.$url); //Location和":"之间无空格。
2. php声明content-type
header('content-type:text/html;charset=utf-8');
3. php返回response状态码
header('HTTP/1.1 404 Not Found');
4. php在某个时间后执行跳转
header('Refresh: 10; url=http://4dn.net/'); //10s后跳转。
5. php控制浏览器缓存
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
6. php执行http验证
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="Top Secret"');
7. php执行下载操作
header('Content-Type: application/octet-stream'); //设置内容类型
header('Content-Disposition: attachment; filename="example.zip"'); //设置MIME用户作为附件
header('Content-Transfer-Encoding: binary'); //设置传输方式
header('Content-Length: '.filesize('example.zip')); //设置内容长度
黑蜘蛛