header
Deepak
Dec 20, 2024
header() function in php used to send raw HTTP header to the requesting client.
- $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
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
.....
....
/**
* Set Content type used to tell the 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"');
...
Scroll for More Useful Information and Relevant FAQs
How to redirect to another page in php?
Define header by providing url of page as below
header("Location: url_of_page_to_redirect");
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();
?>
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');
With header('Content-Type: application/json');
Response required in json but with charset utf-8
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
<?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
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';
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 } ?>