沃梦达 / 编程问答 / php问题 / 正文

在 PHP 中重定向而不使用 header 方法

Redirect in PHP without use of header method(在 PHP 中重定向而不使用 header 方法)

本文介绍了在 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 方法

基础教程推荐