The header() function is a built-in function in PHP that must send a raw HTTP header. The HTTP functions are those functions that manipulate info sent from one page to a different page.
Before any other output has been through. The PHP header() function sends an HTTP header to a client or browser in a raw form.
Before HTML or other output has been sent to a browser or client, raw data is sent with the request (mainly HTTP appeal) made by the server as header information.
HTTP header provides required information about the object and sent in the message body more correct, about the request and response. The first method is 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 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 functions, and have spaces or empty lines that are produced before header() call. The alike problem exists when using a single PHP/HTML file.
Types Of Header:
$header: The header is a string in this parameter. In this article, I will explain two types of header calls.
The first header starts with a string such as “HTTP/”, which is used to reckon out the HTTP status and send the code. The second case of the header is the “Location:”. It is a compulsory parameter.
$replace: It is an optional parameter. It denotes the header should replace the previous or add a second header. The default value is True (will replace). If $replace value is False then it forces 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.
The 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 )
Read more: How To Fix Undefine Index In PHP ( $_POST and $_GET )
Creating a header() function in PHP is actually quite simple. All you need to do is create a new file and name it header.php (or whatever you want to call it), and then add the following code to it:
<?php function header($title, $heading) { echo "<html> <head> <title>$title</title> </head>"; echo "<body bgcolor='white'> <table border='0' cellpadding='10'> <tr bgcolor='#CCCCCC'> <td align='center'> <font size='+2'>$heading</font></td></tr></table></head></html>