表单成功后php重定向

Using php script to allow people to add their email to a mailing list. When it successfully writes the email to the .txt doc it displays a success message on a white page. I want it to redirect to a page I've designed to look like the rest of the site. I've read elsewhere that I need to use the header function, but I can't figure out where to put it since the preceding portion of my code contains outputs.

I think this is the relevant code:

// Write to file if email doesn't already exist
    if ($emailexists != "yes") {

        // Write to the list and give success message
        fwrite($fh, "$_POST[email]
");
        $msg = "Your e-mail address $EmailFix was successfully added to the list!";
    }

    // Close the list
    fclose($fh);
}

Then this:

// Show success message if there was one and end script, and no errors were encountered
} elseif (isset($msg)) {
echo "<b>" . $msg . "</b>
";
echo "</body>
";
echo "</html>
";
exit;
}

Try using

header("Location: http://...");

or

header("Location: filename.php");

Make sure there's no HTML output before calling the header function!

Do you want this message to still show and THEN redirect them somewhere else? Or do you want to discard this message all together and direct them to a different page that will have a similar message?

You could show that page as is, and include another echo to trigger a redirect:

echo "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"2;URL=/someotherpage.php\">";

If your previous code contains code than you can use javascript redirection:

window.location = "YOUR_URL_HERE";

You should norify the user the user that s/he will be redirected.