Quick solution
To quickly redirect to another page using PHP, use this code snippet and change the destination URL to what you want:
header( 'Location: ' . $destinationUrl );
exit;
This snippet will redirect the current page to the designated URL in the $destinationUrl
variable.
Note: It is recommended to use the exit
statement to prevent any other unwanted output to be sent to the client.
There are some other ways to redirect to another page using PHP, combined with HTML & JavaScript, and utilizing status codes for useful redirection cases.
Redirect to another page using PHP
Using header()
function
The header()
function will send an HTTP Request header to the client browser, signifying a destination URL to be redirected to. More about HTTP headers.
header( 'Location: ' . 'https://php101.net/new-url' );
exit;
After a header()
redirection, it is recommended to follow up with the exit
function, as codes after the header()
function might still be sent to the browser, causing issues.
To redirect to another page in php without header
function, we can utilize HTML and JavaScript.
Using JavaScript with window.location.href
We can use PHP combined with HTML & Javascript for redirection using native window.location.href
property, which will execute a redirection to the new URL:
$newUrl = 'https://php101.net/new-url';
echo "<script>window.location.href='{$newUrl}';</script>";
exit;
Using HTML with meta
tag
Another redirect to another page using PHP with HTML is to use the HTML meta
tag. With this method, we can specify the delay time before the redirection happens:
$newUrl = 'https://php101.net/new-url';
$delayTime = 5; // In seconds
echo "<meta http-equiv='refresh' content='{$delayTime}; url={$newUrl}' />";;
exit;
Above are the popular methods for redirection to another page utilizing PHP, HTML & Javascript.
What status code should be used for redirection?
By default, the status code (or response code) of the header()
function is 200
, meaning a successful response with no errors. Sometimes in PHP applications, we want to put specific response codes for notifying the client browser when something happens.
For example, the popular response codes that can be used are:
301
– Moved permanently: when the redirection is permanent, and the new URL replaces the old one.302
– Moved temporarily: when the redirection is temporary, indicates that the URL (before redirection) can be active after some time.400
– Bad request: when there are malformed requests from the client, such as bad syntax.403
– Forbidden: when an URL is forbidden.404
– Not found: when a resource is not to be found or didn’t exist.500
– Internal server error: when an error came from the server that causes failure to respond.
Using these status codes with PHP redirection can be useful, for example, with the redirected page containing the error/information message. Below are some examples of PHP redirection with status codes.
Examples of PHP redirection with status codes
Using 301
status code to notify users or web crawlers that a page has moved permanently:
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://php101.net/new-url");
exit;
Using 302
status code to notify that a page is temporarily moved, and can be accessible again later:
header("HTTP/1.1 302 Moved Temporarily");
header("Location: https://php101.net/new-temporary-url"); // With message notifying the old page will be back
exit;
Using 403
status code to indicate that the page is forbidden to view:
header("HTTP/1.1 403 Forbidden");
header("Location: https://php101.net/error-forbidden"); // With message notifying forbidden error
exit;
Using 404
status code to indicate that a resource is not found:
header("HTTP/1.1 404 Not Found");
header("Location: https://php101.net/not-found"); // With message notifying the previous resource is not found
exit;
Final thoughts
Below are the simple but in-depth ways to redirect to another page using PHP, with multiple examples and use cases. We hope this article is useful for you in your PHP development.
You might also like
- How to display image from file using PHP
- How to count array elements using PHP
- How to download file from URL using PHP