How to Redirect a Web Page with PHP
In PHP, you can use several functions to make a redirect from one web page to another. Learn how to do it accurately with W3docs.
This short snippet shows multiple ways to redirect a web page with PHP. You can achieve redirection by following the guidelines below.
Using the header() Function
This is a built-in PHP function used to send a raw HTTP header to the client.
The syntax of the header() function is as follows:
header( $header, $replace, $http_response_code )This function must be called before any output is sent to the browser.
Let’s see how to redirect a web page using the header() function:
<?php
header('Location: https://www.w3docs.com');
// or die();
exit();
?>As you can notice, exit() is used in the example above. It prevents further script execution, ensuring no additional content is sent to the browser.
Also, you can use the header() function with ob_start() and ob_clean(), like this:
<?php
ob_start(); // Start output buffering
header('Location: target-page.php');
ob_clean(); // Discard buffer contents without sending them
?>Note: PHP 8.1+ includes a built-in http_redirect() function that simplifies this process.
Using a Helper Function
Here, we will demonstrate how to use a helper function for redirecting a web page. Here is an example:
<?php
function Redirect($url, $permanent = false)
{
header('Location: ' . $url, true, $permanent ? 301 : 302);
exit();
}
Redirect('https://www.w3docs.com/', false);All HTTP status codes are listed at HTTP Status Messages.
Note that this function does not support the 303 status code.
Let’s check out a more flexible example:
<?php
function redirect($url, $statusCode = 303)
{
header('Location: ' . $url, true, $statusCode);
die();
}For advanced scenarios involving CLI or CGI/FastCGI environments, you may need to set a Status header explicitly to ensure accurate redirection:
<?php
function Redirect($url, $code = 302)
{
if (strncmp('cli', PHP_SAPI, 3) !== 0) {
if (headers_sent() !== true) {
if (strlen(session_id()) > 0) {
// if using sessions
session_regenerate_id(true); // avoids session fixation attacks
session_write_close(); // avoids having sessions lock other requests
}
if (strncmp('cgi', PHP_SAPI, 3) === 0) {
header(sprintf('Status: %03u', $code), true, $code);
}
header('Location: ' . $url, true, preg_match('~^30[1237]$~', $code) > 0 ? $code : 302);
}
exit();
}
}
?>Using JavaScript for Redirection
Here, we will provide you with an alternative method of client-side redirection using JavaScript. In JavaScript, the window.location object is used to get the current URL and redirect the browser to a new webpage. This object encompasses essential information about a page (for example, href, hostname, and so on).
This is how to redirect a web page using window.location:
<!DOCTYPE html>
<html>
<head>
<title>window.location function</title>
</head>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "URL: " + window.location.href + "<br>";
document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + "Hostname: " + window.location.hostname + "<br>";
document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + "Protocol: " + window.location.protocol + "<br>";
// Redirect to a new page
window.location.href = "https://www.w3docs.com";
</script>
</body>
</html>To conclude, this tutorial has covered multiple methods to redirect a web page with PHP. You can also find information on how to redirect web pages using HTML, JavaScript, Apache, and Node.js.