10

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?

4
  • 4
    header() isn't complex at all, use it like this header('Location: index.html'); Commented Mar 14, 2013 at 14:27
  • 2
    header("Location: index.html"); is the simplest way to redirect someone on the server side. What problems are you encountering with it? Commented Mar 14, 2013 at 14:28
  • @StevenVondruska , I haven't had a problem yet, but the manuel seems to suggest I have to put the header() at the beginning of the script. So, my concern is the balance of the script won't get executed. Commented Mar 14, 2013 at 17:09
  • You can put header() anywhere in the script. The trick is that you cannot send any content before you call header(). So you can't do something like echo "hello world"; header("Location: index.html"); Commented Mar 14, 2013 at 19:00

3 Answers 3

19

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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>

3 Comments

JavaScript can be disabled by user.
I agree, which is why I stated it wasn't the best way. Using php with header('Location: index.html') would be the preferred way, although the user stated it was causing interference with their code.
Commenting JS code like that is for ancient browsers ...
1

Just echo this javascript code end of the process.

    <script>
      window.location.href = 'http://www.yourwebsite.com';
    </script>

3 Comments

What if Javascript is disabled ?
<meta http-equiv="refresh" content="0;URL='yourwebsite.com'"> content means time , yes Shivan this is an alternative solution.
Meta refresh is a sloppy solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.