http_response_code()和header()

Posted laijinquan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了http_response_code()和header()相关的知识,希望对你有一定的参考价值。

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()的主要内容,如果未能解决你的问题,请参考以下文章

PHP 页面跳转的三种方式

PHP header函数的几大作用

PHP header函数的几大作用

检查PHP中是否超出了最大上传文件数

[单选题]那个函数可以检查HTTP头是否已经发送或者发送到哪儿?

git中HEAD^和HEAD~区别