Home Server http_response_code() and header()

http_response_code() and header()

131
0
http_response_code()
http_response_code()

1.http_response_code

Get/set the HTTP status code of the response.

Send a successful status code to the server: http_response_code(200);

Return Value

If response_code is provided, the previous status code will be returned. If response_code is not provided, the current status code will be returned. In a Web server environment, the default value of these status codes is  200 .

<?php
// Get the current status code and set a new status code
var_dump(http_response_code(404));//Get the new status code
var_dump(http_response_code());
?>

The above routine will output:

int(200)
int(404)

2. The header() function

sends the original HTTP header to the client.

It’s important to realize that the header() function must be called before any actual output is sent (in PHP 4 and higher, you can use output caching to solve this problem):
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'); //Send success status code 200 to the server
Here $_SERVER['SERVER_PROTOCOL'] is the name and version of the communication protocol when requesting the page. For example: 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

Get all headers sent by the server in response to an HTTP request

Description
get_headers​get_headers ( string $url [, int $format = 0 ] ) : array​ () returns an array containing the headers sent by the server in response to an HTTP request.

<?php
$url = 'http://www.example.com';
print_r(get_headers($url));
print_r(get_headers($url, 1));
?>

The output of the above routine is similar to:
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

Get all HTTP request header information

Get all request header information for the current request

<?php
$headers = apache_request_headers();

foreach ($headers as $header => $value) {
    echo "$header: $value <br />\n";
}
?>
The output of the above routine is similar to:

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

Get all HTTP response header information.

<?php
print_r(apache_response_headers());
?>

Array
(
    [Accept-Ranges] => bytes
    [X-Powered-By] => PHP/4.3.8
)

LEAVE A REPLY

Please enter your comment!
Please enter your name here