Redirect in PHP without use of header method(在 PHP 中重定向而不使用 header 方法)
问题描述
此contact.php 表单正在处理提交,然后重定向到新页面,然后突然停止工作.我尝试添加错误处理并将标题移动到所有其他标题的顶部,但两者都不起作用.表单仍在按预期提交数据,只是重定向不起作用.任何想法将不胜感激.
This contact.php form was working to handle a submit and then redirect to a new page and then all of a sudden just stopped working. I have tried adding error handling and also moving header to the top in front of all other, but neither works. The form is still submitting the data as expected, it's just the redirect that doesn't work. Any ideas would be appreciated.
<?php
include 'config.php';
$post = (!empty($_POST)) ? true : false;
if($post)
{
$email = trim($_POST['email']);
$subject = "Downloaded Course Units";
$error = '';
if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $subject, $message,
"From: ".$email."
"
."Reply-To: ".$email."
"
."X-Mailer: PHP/" . phpversion());
if($mail)
{
echo 'OK';
header('location: http://www.google.com.au/');
exit();
}
}
}
?>
推荐答案
使用javascript.
Use javascript.
代替
header('location: http://www.google.com.au/');
使用,
?>
<script type="text/javascript">
window.location.href = 'http://www.google.com.au/';
</script>
<?php
即使你的浏览器有输出,它也会重定向.
It will redirect even if something is output on your browser.
但是,要采取一项预防措施:即使页面上打印了某些内容,Javascript 重定向也会重定向您的页面.
But, one precaution is to be made: Javascript redirection will redirect your page even if there is something printed on the page.
确保它不会跳过任何用 PHP 编写的逻辑.
Make sure that it does not skip any logic written in PHP.
这篇关于在 PHP 中重定向而不使用 header 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 PHP 中重定向而不使用 header 方法
- 从 PHP 中的输入表单获取日期 2022-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- Laravel 仓库 2022-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01