1.http_response_code — 获取/设置响应的 HTTP 状态码
向服务器发送成功状态码:http_response_code(200);

返回值
如果提供了response_code,将返回先前的状态码。 如果未提供response_code,会返回当前的状态码。 在 Web 服务器环境里,这些状态码的默认值都是 200

<?php
// 获取当前状态码,并设置新的状态码
var_dump(http_response_code(404));//获取新的状态码
var_dump(http_response_code());
?> 以上例程会输出: int(200)
int(404)

2.header() 函数向客户端发送原始的 HTTP 报头。

认识到一点很重要,即必须在任何实际的输出被发送之前调用 header() 函数(在 PHP 4 以及更高的版本中,您可以使用输出缓存来解决此问题):
header(string,replace,http_response_code)

header("HTTP/1.0 404 Not Found");
header("Location: http://www.example.com/");
header('Content-type: application/pdf'); header('HTTP/1.1 301 Moved Permanently');
header("HTTP/1.1 404 Not Found");
header("Location: http://www.kongjianjia.com".$_SERVER['REQUEST_URI']);
header("Location:".$_SERVER["SERVER_NAME"]);//$_SERVER["HTTP_HOST"] // 301 Moved Permanently
header("Location: /foo.php",TRUE,301); // 302 Found
header("Location: /foo.php",TRUE,302);
header("Location: /foo.php"); // 303 See Other
header("Location: /foo.php",TRUE,303); // 307 Temporary Redirect
header("Location: /foo.php",TRUE,307); // It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"'); // The PDF source is in original.pdf
readfile('original.pdf'); file() header("Cache-Control: no-cache, must-revalidate");
<?php
/* Redirect to a different page in the current directory that was requested */
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;
?>
header($_SERVER['SERVER_PROTOCOL'] . ' 200 OK'); //向服务器发送成功状态码200
这里$_SERVER['SERVER_PROTOCOL']就是请求页面时通信协议的名称和版本 例如:HTTP/1.1 header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error');
<?php
// Test image.
$fn = '/test/foo.png'; // Getting headers sent by the client.
$headers = apache_request_headers(); // Checking if the client is validating his cache and if it is current.
if (isset($headers['If-Modified-Since']) && (strtotime($headers['If-Modified-Since']) == filemtime($fn))) {
// Client's cache IS current, so we just respond '304 Not Modified'.
header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($fn)).' GMT', true, 304);
} else {
// Image not cached or cache outdated, we respond '200 OK' and output the image.
header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($fn)).' GMT', true, 200);
header('Content-Length: '.filesize($fn));
header('Content-Type: image/png');
print file_get_contents($fn);
}

3.get_headers — 取得服务器响应一个 HTTP 请求所发送的所有标头

说明
get_headers ( string $url [, int $format = 0 ] ) : array
get_headers() 返回一个数组,包含有服务器响应一个 HTTP 请求所发送的标头。

<?php
$url = 'http://www.example.com';
print_r(get_headers($url));
print_r(get_headers($url, 1));
?> 以上例程的输出类似于:
Array
(
[0] => HTTP/1.1 200 OK
[1] => Date: Sat, 29 May 2004 12:28:13 GMT
[2] => Server: Apache/1.3.27 (Unix) (Red-Hat/Linux)
[3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
[4] => ETag: "3f80f-1b6-3e1cb03b"
[5] => Accept-Ranges: bytes
[6] => Content-Length: 438
[7] => Connection: close
[8] => Content-Type: text/html
) Array
(
[0] => HTTP/1.1 200 OK
[Date] => Sat, 29 May 2004 12:28:14 GMT
[Server] => Apache/1.3.27 (Unix) (Red-Hat/Linux)
[Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT
[ETag] => "3f80f-1b6-3e1cb03b"
[Accept-Ranges] => bytes
[Content-Length] => 438
[Connection] => close
[Content-Type] => text/html
)

4.apache_request_headers — 获取全部 HTTP 请求头信息
获取当前请求的所有请求头信息

<?php
$headers = apache_request_headers(); foreach ($headers as $header => $value) {
echo "$header: $value <br />\n";
}
?>
以上例程的输出类似于: Accept: */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0
Host: www.example.com
Connection: Keep-Alive

5.apache_response_headers ---获得全部 HTTP 响应头信息。

<?php
print_r(apache_response_headers());
?> Array
(
[Accept-Ranges] => bytes
[X-Powered-By] => PHP/4.3.8
)

http_response_code()和header()的更多相关文章

  1. PHP 模拟 HTTP 摘要认证(Digest )

    <?php header("Content-type: text/html; charset=utf-8"); /*php摘要认证*/ $users = ['dee'=> ...

  2. PHP 模拟 HTTP 基本认证(Basic Authentication)

    当某个页面需要认证才能进行访问时,接到请求后服务器端会在响应头中发送一个 WWW-Authenticate 首部(用来标识认证安全域),语法为 WWW-Authenticate:Basic relam ...

  3. PHP 面试知识点整理归纳

    基础篇了解大部分数组处理函数 array_chunk — 将一个数组分割成多个    array_column — 返回数组中指定的一列    array_combine — 创建一个数组,用一个数组 ...

  4. php文件编程

    一:文件常见操作 流的概念:当数据从程序(内存)->文件(磁盘),我们称为输出流,当数据从文件(磁盘)->程序(内存),我们称为输入流 1,获取文件信息 <?php //打开文件 f ...

  5. PHP实现服务器文件预览

    PHP实现服务器里面的所有文件进行预览跟手机文件夹一样 服务器创建一个index.php文件 点我查看 <?php // errors ini_set('display_errors', 1); ...

  6. PHP中的header()函数作用

    PHP 中 header()函数的作用是给客户端发送头信息. 什么是头信息?这里只作简单解释,详细的自己看http协议.在 HTTP协议中,服务器端的回答(response)内容包括两部分:头信息(h ...

  7. php:Header

    转自鸟哥的博客: http://www.laruence.com/2007/12/16/308.html PHP header()the function declaration: void head ...

  8. PHP header函数的几大作用

    先看看官方文档的定义 (PHP 4, PHP 5, PHP 7) header - 发送原生 HTTP 头 void header ( string $string [, bool $replace ...

  9. PHP header() 函数详细说明(301、404等错误设置)

    原文来自:http://www.veryhuo.com/a/view/41466.html 如果您刚刚开始学习PHP,可能有许多函数需要研究,今天我们就来学习一下PHP Header()的使用方法,更 ...

随机推荐

  1. 有效括号算法题(Golang实现)

    有效括号算法题 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合.左括号必须以正确的顺序闭合.注意空 ...

  2. Python input和print函数

    一.input函数 可以看出,input()函数默认输入的是字符串类型,需要eval()函数将其进行转换. 区别直接赋值的情况,Python可以自动识别数据类型 二.print函数 1.直接输出 无论 ...

  3. Mob 之 短信验证集成 SMSSDK

    开相关发中总会遇到短信验证这些操作,这周没有来得及写新的东西,借此分享一篇以前学习短信验证的笔记,本文使用的是 Mob 提供的 SMSSDK . 下载 SMSSDK 官网下载地址:SMSSDK 集成 ...

  4. RbbitMQ详解

    高性能消息队列RabbitMQ 1.为什么要使用mq 主要解决应用解耦,流量削峰,异步消息,实现高性能,可升缩,最终一致性的架构. 2.activeMq的通讯模式 基于队列(点对点)与发布订阅(有多个 ...

  5. NKOJ4330 逛公园

    时间限制 : - MS   空间限制 : 565536 KB  评测说明 : 3s 问题描述 策策同学特别喜欢逛公园.公园可以看成一张N个点M条边构成的有向图,且没有 自环和重边.其中1号点是公园的入 ...

  6. 阿里 IOS 面试官教你在面试中脱颖而出

    前言: 知己知彼.百战不殆,面试也是如此. 只有充分了解面试官的思路,才能更好地在面试中充分展现自己. 今天,阿里高级技术专家将分享自己作为面试官的心得与体会.如果你是面试者,可以借此为镜,对照发现自 ...

  7. LinkedHashMap 与 HashMap 实现的区别

    阅读前最好对 HashMap 的内部实现方式有一定了解 LinkedHashMap 继承自 HashMap 主要重写了一个节点类 LinkedHashMap.Entry,并维护一个头结点和尾节点 以及 ...

  8. 下载安装配置 Spark-2.4.5 以及 sbt1.3.8 打包程序

    文章更新于:2020-03-29 按照惯例,文件附上链接放在文首. 文件名:spark-2.4.5-bin-without-hadoop.tgz 文件大小:159 MB 下载链接:https://mi ...

  9. 天天写order by,你知道Mysql底层执行原理吗?

    前言 文章首发于微信公众号[码猿技术专栏]. 在实际的开发中一定会碰到根据某个字段进行排序后来显示结果的需求,但是你真的理解order by在 Mysql 底层是如何执行的吗? 假设你要查询城市是苏州 ...

  10. sqlchemy查询的其他操作

    sqlalchemy的数据查询排序 1 .正序排序:session.query(model).order_by(model.attr).all() session.query(model).order ...