Redirect in PHP

2019-09-05 15:18发布

I have a typical mail.php script, which uses the mail() function. After the user inputs information into a typical HTML form, I have the mail() function send the email to the desired email [with mail($email_of_client) etc etc]

My question is this:

After the email sends, I want the user to be redirected to a ThankYou page (run under wordpress, so it doesn't have .html or .php extensions)

I tried the following:

<meta http-equiv="refresh" content="0;URL=http://my-site-here.com/thankyou">

This does the job, but shows a blank screen for a millisecond. I was wondering if it's possible to do a right away redirect after the user inputs the data and clicks on send.

Thanks a lot,

Amit

3条回答
贪生不怕死
2楼-- · 2019-09-05 15:47

You can do a redirect using HTTP Headers. Use something like this:

<?php
  header('Location: http://www.example.com/');
?>

Note that you cannot have any output before the call to header (unless you have output buffering).

See http://php.net/manual/en/function.header.php for details on the function, and general information about the http headers.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-09-05 16:02

Instead of using the meta refresh, use

header('Location: http://my-site-here.com/thankyou');
exit();

Do not forget about exit() if you don't want to execute what's after that line

This works if you didn't sent anything to the browser. If you had to send anything to the browser the solution is:

ob_start();
echo "sending something to the browser";
header('Location: http://my-site-here.com/thankyou');
ob_end_clean();
exit();
查看更多
倾城 Initia
4楼-- · 2019-09-05 16:03

To add to the other answers, it's always a good idea to send a status code too;

header('HTTP/1.1 302 Found');
header('Location: http://example.com'); 
查看更多
登录 后发表回答