How Can I Do a Redirect to Different URL Before Page Load?
Let’s say that you want all the visitor on the webpage
$new_url = 'https://example.com/final.php';
Using the PHP header() Function to Redirect a URL
If you want to redirect
final.php, you can put the following code inside the
initial.php webpage. This code sends a new HTTP location header to the browser.
$new_url = 'https://example.com/final.php'; header('Location: '.$new_url);
Here we have use the header() function in PHP to make the redirect. The important thing that you should keep in mind is that the above code should be placed before any HTML or text output. Otherwise, you will get an headers already sent error. You can also use output buffering in order to get rid of the headers already sent error. The following example shows it in action:
ob_start(); $new_url = 'https://example.com/final.php'; header('Location: '.$new_url); ob_end_flush();
ob_start() function should be the first function in your PHP script when you want to perform a redirect using the header() function in PHP. This will prevent the headers already sent error from occurring.
As a safety measure, you might want to add
exit() directly after the header redirect in order to prevent the rest of the code of the webpage from executing. Sometimes, a crawler or a browser will not respect the Location header and this might jeopardize the security of your website.
$new_url = 'https://example.com/final.php'; header('Location: '.$new_url); exit();
To be clear,
exit() have nothing to do with the redirection. They are used to prevent the rest of the code on the webpage from executing.
It is also recommended to use absolute URLs while specifying a Location header value. However, relative URLs will also work just fine. You can also use this function to redirect users to external websites or webpages.
This is not a pure PHP based solution but that doesn’t diminishes its effectiveness. You can use the PHP
echo "<script>self.location='https://example.com/final.php';</script>"; echo "<script>document.location.href='https://example.com/final.php';</script>"; echo "<script>window.location.href='https://example.com/final.php';</script>"; echo "<script>window.location.replace('https://example.com/final.php');</script>";
Using HTML meta Tags to Redirect a Webpage
<meta http-equiv="Location" content="http://example.com/final.php"> <!-- The following line will redirect after the given number of seconds. Zero in our case. --> <meta http-equiv="refresh" content="0;url=http://example.com/final.php">
You can also use the last line from the previous code block in order to auto-refresh a page every “n” seconds. For example, the following line will automatically refresh the page after every 8 seconds.
<meta http-equiv="refresh" content="8">
Let’s recap everything that we have covered in this tutorial.
- The easiest way to redirect users to a different page in PHP is to use the
header()function. It is important to call this function before any HTML output in order to avoid headers already sent error.
- Another way to avoid the headers already sent error is to use PHP
- One final method to redirect users to a different URL is to use HTML meta tags. These tags can also be used to refresh the same webpage after ‘n’ number of seconds.
Let me know if there is anything that you would like me to clarify in this tutorial. Also, you are more than welcome to comment if you know other techniques to redirect your users to a different page in PHP.
Rate this post —