header

  • June, 2021
  • 0 Views
  • 3
  • 2

header() function in php used to send raw HTTP header to the requesting client. 

Definition : header($header, $replace, $http_response_code)
  • $header : Required param. Type : String
  • $replace : Optional param. Used to replace with previous same kind of header or add new same type of header. Default value : true ( true mean it will replace with previous)
  • $http_response_code : Optional param. This param is used to force response code to given value. Default : 0
Here is the example for more understanding

Example 1 : Send Status Code
<?php
	header("HTTP/1.0 404 Page Not Found");
?>
Send Code 404 as below

Request Method: GET
Status Code: 404 Page Not Found
.....
....
Example 2 : Send content type for Download file
/**
 *  Set Content type used to tell client about media type, example below for xml
 */
header('Content-type: text/xml');

/**
 *  Content-Disposition : attachment mean this file specified should download
 */
header('Content-Disposition: attachment; filename="download.xml"');
...

1. How to redirect to another page in php?

Define header by providing url of page as below

header("Location: url_of_page_to_redirect");

<?php

/**
 * Example 1:  If Redirecting to another website
 */
header('Location: https://www.quizcure.com');
exit();
?>

<?php

/**
 * If Redirecting to another page within same domain
 */
header('Location: landingPage.php');
exit();
?>

No comments posted yet. Be the first to post comments. Thanks

Write Your Comments

2. How to use header() function to return Json response

Content-Type used to tell client about media type. For json type we need to define header as below

header('Content-Type: application/json');

Example :

Without using header('Content-Type: application/json');

<?php

$array = array(
    "demo" => array("demo1", "demo2"),
    "xyz" => array("xyz"),
    "abc" => array("abc1", "abc2", "abc3")
);

echo json_encode($array, true);
exit;

?>

/**
 *  Content-Type in Response Header :
 */

Content-Type: text/html; charset=UTF-8


With header('Content-Type: application/json');
<?php

$array = array(
    "demo" => array("demo1", "demo2"),
    "xyz" => array("xyz"),
    "abc" => array("abc1", "abc2", "abc3")
);

/** Tell client about response in json **/

header('Content-Type: application/json'); 
echo json_encode($array, true);
exit;
?>
<br>
/**
 *  Content-Type in Response Header :
 */

Content-Type: application/json
Response required in json but with charset utf-8

<?php

$array = array(
    "demo" => array("demo1", "demo2"),
    "xyz" => array("xyz"),
    "abc" => array("abc1", "abc2", "abc3")
);

/** Tell client about response in json utf-8 <br> **/

header('Content-Type: application/json;charset=utf-8'); 
echo json_encode($array, true);
exit;
?>
/**
 *  Content-Type in Response Header :
 */

Content-Type: application/json;charset=utf-8

Write Your Comments

3. How to redirect without using php header() function?

We can use javascript code as well to redirect to another page instead of using php header function.

Here is code to redirect

window.location.href = 'url_to_redirect';
<?php
/**
 *  Any PHP Code here like 
 */
if (isset($_POST['submit'])) {

    /**
     * Some manipulation with submitted data received in post like database update or other calculation
     */
    ?>
    <!-- Redirect to web page as applicable -->
    <script type="text/javascript">
        window.location.href = 'https://www.quizcure.com';
    </script>

<?php } ?>

Write Your Comments