PHP 101
  • Home
  • Deploy
  • How-to
  • Tutorial
No Result
View All Result
PHP 101
  • Home
  • Deploy
  • How-to
  • Tutorial
No Result
View All Result
PHP 101
No Result
View All Result
Home How-to

How to redirect to another page using PHP

Guide on how to redirect to another page or URL using PHP.

March 8, 2023
Reading Time: 4 mins read
PHP101.net - How to redirect to another page using PHP

PHP101.net - How to redirect to another page using PHP

Share on FacebookShare on Twitter

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

References

  • PHP.net Official Documentation – header() function
  • HTTP headers – Mozilla Developer Resources
  • Wikipedia – 3xx redirection status codes
Tags: 301301 status code302400403404php html javascript redirectphp html redirectphp javascript redirectphp redirectphp redirect to another pagephp redirect to urlstatus codes
Previous Post

Deploy a webserver with Laragon on Windows for PHP development

Next Post

How to set timezone with PHP easily (multiple methods)

Related Posts

PHP101.net - How to set timezone with PHP

How to set timezone with PHP easily (multiple methods)

March 9, 2023
PHP101.Net - How to remove empty array elements using PHP

How to remove empty array elements using PHP

September 18, 2022

Table of Contents

    • Quick solution
    • Redirect to another page using PHP
    • Using header() function
    • Using JavaScript with window.location.href
    • Using HTML with meta tag
    • What status code should be used for redirection?
    • Examples of PHP redirection with status codes
    • Final thoughts
    • You might also like
    • References
Next Post
PHP101.net - How to set timezone with PHP

How to set timezone with PHP easily (multiple methods)

We deliver free and helpful resources for learning
and using PHP programming language.

About
Contact Us
Privacy Policy
Main Sections

Home
Deploy
How-to
Tutorial
You might be interested in...

How to display image from file using PHP

How to set timezone with PHP easily (multiple methods)

How to install PHP GeoIP extension & GeoLite2 library (databases included)

How to convert from string to array in PHP

How to Write PHP Comments (and best practices)

© 2022 PHP101.Net - Helpful PHP Tutorials, How-to, Tips and Tricks for everyone.

No Result
View All Result
  • Home
  • Deploy
  • How-to
  • Tutorial

© 2022 PHP101.Net - Helpful PHP Tutorials, How-to, Tips and Tricks for everyone. | Privacy Policy

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.