Home >>Advance PHP Tutorial >PHP Header Function

PHP Header Function

PHP | header () Function

The PHP header () function is used to send a raw HTTP header to a client. It is an inherent function in PHP.

The HTTP functions are those functions which manipulates information sent to the Web server to the browser or client, before any actual output has been sent.

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

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

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

<?php 
echo "we will redirect to Phptpoint Official website in 3 second"; 
// The function will redirect to Phptpoint official website 
header("location:http://www.phptpoint.com"); 
?> 

Example(Enter Your valid email id it will redirect on phptpoint main website)

<?php
extract($_REQUEST);
if(isset($save))
{
	if($eid=="[email protected]")
	{
	//redirecting on phptpoint main website	
	header('location:https://www.phptpoint.com');	
	}
	else
	{
	echo "Pls enter valid email id";	
	}
}
?>
<!DOCTYPE html>
<html>
<head>
	<title>Header Function in PHP</title>
</head>
<body>
<form action="#">
	<table>
		<tr>
			<td>Enter Your Email</td>
			<td><input type="email" value="[email protected]" name="eid"/></td>
		</tr>
		<tr>
			<td colspan="2" align="center">
			<input type="submit" value="save" name="save"/></td>
		</tr>
	</table>
</form>
</body>
</html>

Parameter Values

The PHP Header () function contains three parameter which can be described below:

$header: It requires header string to send. This parameter is mandatory.

$replace: It denotes the header should replace previous similar header or add a new second header of the same type. TRUE (will replace) is the default value. Multiple same type header allows, if $replace value is false.This parameter is optional.

$http_response_code: It is an optional parameter. It indicates whether response code is success or not to the specified value.

Refresh a Page Using PHP

The HTTP functions are those functions which manipulates information sent to the Web server, to the browser or client, before any actual output has been sent.

The PHP header () function is used to send a raw HTTP header to a client. It provides required information regarding the object sent in the message body more precisely about the response and request.

<?php 
  // Demonstrate the use of header() function 
// to refresh the current page 
   echo "Welcome to index page </br>"; 
echo "we will redirect to Phptpoint Official website in 3 second"; 
    // The function will redirect to Phptpoint official website 
header("refresh: 3; url = https://www.phptpoint.com/"); 
   exit; 
?> 

Example(Enter Your valid email it print a message like you will redict after 3 seconds and then redirect)

<?php
extract($_REQUEST);
if(isset($save))
{
	if($eid=="[email protected]")
	{
	//redirecting on phptpoint main website	after 3 seconds
	header('refresh:3;url=https://www.phptpoint.com');	
	echo "Congrates ! you will redict on main website after 3 seconds";
	}
	else
	{
	echo "Pls enter valid email id";	
	}
}
?>
<!DOCTYPE html>
<html>
<head>
	<title>Header Function in PHP</title>
</head>
<body>
<form action="#">
	<table>
		<tr>
			<td>Enter Your Email</td>
			<td><input type="email" value="[email protected]" name="eid"/></td>
		</tr>
		<tr>
			<td colspan="2" align="center">
			<input type="submit" value="save" name="save"/></td>
		</tr>
	</table>
</form>
</body>
</html>
Output :