How to make header function in PHP?

The header() function is an built-in function in PHP which must be send a raw HTTP header. The HTTP functions are those functions which manipulate info sent one page to different page.

Before any other output has been through. The PHP header() function send a HTTP header to a client or browser in a raw form.

Before HTML or other output has been sent to a browser or client, a raw data is sent with request (mainly HTTP appeal) made by the server as header information.

HTTP header provide required information about the object and sent in the message body more correct, about the request and response. First method in below.

<?php
# PHP program to describes header function

# Redirect the browser
header("Location: https://wholeblogs.com");

# The below code does not get executed 
# while redirecting
exit;

?>

OUTPUT:

This will redirect your location of header, i.e. redirect to the URL: which URL you gave between header("");

Please remember This header() must be called before any actual production is sent, whether by HTML tags, blank lines in a file, or from PHP. This is a very common error to read the code with include, or require, functions, or different file access function, and have spaces or empty lines that are production before header() is called. The alike problem is exists when using a single PHP/HTML file. see more explain What is header.

 

Types Of Header:

$header: The header is string in this parameter. In this article i will explain two types of header calls.

The first header starts with string such as “HTTP/”, which is used to reckon out the HTTP status and send the code. The second case of header is the “Location:”. It is compulsory 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.

Second method is Print_r; header function in PHP

 

<?php
// PHP program to describes header function

// Set a past date
header("wholeblogs.com: friends, 10 July 1999 3:11:00 am");
header("wholeblogs.com");
header("hello google!");
?>

<html>
<body>
<p>Hello World!</p>

<!-- PHP program to display
header list -->
<?php
print_r(headers_list());
?>

OUTPUT:

Hello World!

Array ( [0] => X-Powered-By: PHP/7.3.9 [1] => wholeblogs.com: monday, 10 Jul 1999 03:40:00 PM [2] => PHP: programing )

header-phpRead more: How To Fix Undefine Index In PHP ( $_POST and $_GET )

 

Leave a Comment