I'm setting up a simple landing page on DreamHost. It won't let me put php code in the file index.html
. So, when user submits an email address, I use $_POST
to submit the email address to another page mail_auto.php
.
After seeing a quick "email sent" message, I'd like the user to be directed back to the index.html
page from mail_auto.php
.
header()
looks a bit complex and seems to interfere with the execution of the balance of mail_auto.php
.
What's the best way to redirect the user?
Answer: Use the PHP header() Function You can simply use the PHP header() function to redirect a user to a different page. The PHP code in the following example will redirect the user from the page in which it is placed to the URL http://www.example.com/another-page.php .
To redirect one HTML page to another page, you need to add a <meta> tag inside the <head> section of the old HTML page.
To set a permanent PHP redirect, you can use the status code 301. Because this code indicates an indefinite redirection, the browser automatically redirects the user using the old URL to the new page address.
To redirect user back to index.html, use the following:
header('Location: index.html');
exit;
Alternatively, if you want to display something like " Redirecting... " on screen, you can use the meta-refresh
method , or JavaScript window.location
method with setTimeout
The meta refresh method:
Add this to HTML <head>
:
<meta http-equiv="refresh" content="2;url=index.html">
where 2 is number of seconds before the refresh is executed.
Just echo this javascript code end of the process.
<script>
window.location.href = 'http://www.yourwebsite.com';
</script>
Using the header is typically what I'd do.
Have you thought about using JavaScript? It's not the best way, although it would work.
<script type="text/javascript">
<!--
window.location = "http://www.google.com/"
//-->
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With