PHP header()

The header() is a pre-defined network function of PHP, which sends a raw HTTP header to a client. One important point to be noted about the header() function is that it must be called before sending any actual output.

The header() function sends an HTTP header in raw form to the client or browser. Before sending any other output, the HTTP functions manipulate the information sent by the web-server to the client or browser.

Syntax

snippet
void header (string $header, boolean $replace = TRUE, int $http_response_code)

Parameters

The header() function accepts three parameters, which are discussed below in detail:

$header (mandatory)

The header parameter contains the header string to send. This function has two types of special header calls.

The first header starts with an "HTTP/" string, which locates the HTTP status code for sending.

The second special case of header starts with "Location:" header. It not only sends back the header to the browser, but it also provides a REDIRECT (302) status code to the browser, until the 201 or 3xx status code has already been set.

$replace (optional)

This parameter is used for specifying whether a previous same header should be replaced by the header or add another header of same type. The $replace is a boolean type optional parameter.

The default value is TRUE, which means that it replaces the previous same header. But you can bind several headers of same type if FALSE is passed as second argument.

http_response_code (optional)

The $http_response_code is an optional parameter, which forces the HTTP response code to a specified value.

Note
Note: The http_response_code parameter has an effect if the header is not blank.

Return Value

The PHP header() function does not return any value.

Changes

After PHP version 5.1.2, this function stops sending more than one header to prevent the header injection attacks. It allows only one header at a time.

Uses

  • It changes the page location.
  • It sets the time zone.
  • It sends the STOP status.
  • This function sets the caching control.
  • It initiates the force download.

Examples

With the help of below examples, you can understand the actual working of header() function in runtime environment-

Example 1: Redirecting browser

The following code will redirect your user to some another page.

snippet
<?php
	// This will redirect the user to the new location
	header('Location: http://www.javatpoint.com/');
	
	//The below code will not execute after header
	exit;
?>

Output

It will redirect to the new URL location, which is given in header() function of the above program, i.e., www.javatpoint.com. If any line of code is written after the header(), it will not execute.

PHP header()

Example 2: Redirection interval

The following code will redirect your user to another page after 10 seconds.

snippet
<?php
	// This will redirect after 10 seconds
	header('Refresh: 10; url = http://www.javatpoint.com/');
	exit;
?>

Output

Output
The output will be same as the example 1, but it will take 10 seconds to load.
Note
Note: If any line of code is written after the header() function, it will not execute.

Example 3: Don't cache pages

By using the following code, you can prevent the browser to cache pages.

snippet
<?php
	// PHP program to describes header function 
  
	// Set a past date 
	header("Expires: Tue, 03 March 2001 04:50:34 GMT"); 
	header("Cache-Control: no-cache"); 
	header("Pragma: no-cache"); 
?>
<html> 
	<body> 
	<p>Hello Javatpoint!</p> 
      
	<!-- PHP program to display 
	header list --> 
	<?php 
		print_r(headers_list()); 
	?> 
	</body> 
</html>

Output

Output
Hello Javatpoint! Array ( [0] => X-Powered-By: PHP/7.3.13 [1] => Expires: Tue, 03 March 2001 04:50:34 GMT [2] => Cache-Control: no-cache [3] => Pragma: no-cache )

Absolute URI

Absolute URI is required by some older clients, which includes the hostname, scheme, and absolute path, whereas most contemporary clients accept relative URI as argument to Location. To make absolute URI, you can use $SERVER['PHP_SELF'], $SERVER['HTTP_HOST'], and dirname().

Example 4

Create two php files, one of which for containing header file code and another for redirecting to a new page on the browser.

headercheck.php

snippet
<?php
	$host  = $_SERVER['HTTP_HOST'];
	$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
	$newpage = 'welcome.php';
	
	/* Redirect to a different page requested in the current directory*/
	header("Location: http://$host$uri/$newpage");
	exit;
?>

welcome.php

snippet
<!-- welcome.php file redirect to new page -->
<html>
		<head>
			<title> Welcome page </title>
		</head>
		<body>
			<h1> <center> Welcome to javaTpoint </center> </h1>
		</body>
</html>

Output

PHP header()
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents