PHP header() Function

The PHP header() function is an inbuilt function in PHP which is used to send a raw HTTP header. The HTTP functions are those functions which manipulate information sent to the client or browser by the Web server, before any other output has been sent. One important point to be noted about the header() function is that it must be called before sending any actual output.

The header() function sends an HTTP header in raw form to the client or browser. Before sending any other output, the HTTP functions manipulate the information sent by the web-server to the client or browser.

Syntax - 

void header( $header, $replace = TRUE, $http_response_code )

Parameters

This function accepts three parameters as mentioned above and described below:

$header: This parameter hold the header string. There are two types of header calls. The first header starts with string “HTTP/”, which is used to figure out the HTTP status code to send. The second case of header is the “Location:”. It is mandatory parameter.

$replace: It is optional parameter. It denotes the header should replace previous or add a second header. The default value is True (will replace). If $replace value is False then it force multiple headers of the same type.

$http_response_code: It is an optional parameter. It forces the HTTP response code to the specified value (PHP 4.3 and higher).

Return Values: This function doesn’t return any value.

Changes

After PHP version 5.1.2, this function stops sending more than one header to prevent the header injection attacks. It allows only one header at a time.

Uses

It changes the page location.

It sets the time zone.

It sends the STOP status.

This function sets the caching control.

It initiates the force download.

Example 1 - 

The following code will redirect your user to some another page.

<?php  

    // This will redirect the user to the new location  

    header('Location: https://wartalab.blogspot.com/');  

    //The below code will not execute after header  

    exit;  

?>  


No comments:

Write a program in PHP to reverse a number

A number can be written in reverse order. For example 12345 = 54321 <?php   $ num = 23456;   $ revnum = 0;   while ($ num > 1)   {   $...