在不使用标头方法的情况下在PHP中重定向 [英] Redirect in PHP without use of header method

查看:101
本文介绍了在不使用标头方法的情况下在PHP中重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个contact.php表单正在处理一个提交,然后重定向到一个新页面,然后突然停止工作。我曾尝试添加错误处理,并将标题移至所有其他顶部的顶部,但都无效。表单仍然按预期提交数据,只是重定向不起作用。任何想法将不胜感激。

 <?php 
include'config.php';
$ post =(!empty($ _ POST))?真假;
if($ post)
{
$ email = trim($ _ POST ['email']);
$ subject =已下载的课程单元;
$ error ='';
if(!$ error)
{
$ mail = mail(WEBMASTER_EMAIL,$ subject,$ message,
From:。$ email。\r\ n
回复:。$ email。\r\\\

。X-Mailer:PHP /。phpversion());
if($ mail)
{
echo'OK';
标题('location:http://www.google.com.au/');
exit();
}
}
}
?>


解决方案

使用javascript。

而不是

 标题('location:http://www.google.com.au/ ); 

使用,

 >?; 
< script type =text / javascript>
window.location.href ='http://www.google.com.au/';
< / script>
<?php

即使您的浏览器输出了内容,它也会重定向。 / p>

但是,一个预防措施是:Javascript重定向将重定向您的页面,即使页面上印有某些内容。



确保它不会跳过以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."\r\n"
       ."Reply-To: ".$email."\r\n"
       ."X-Mailer: PHP/" . phpversion());   
    if($mail)
    {
    echo 'OK';
    header('location: http://www.google.com.au/');
    exit();
    }
    }
    }
    ?>

解决方案

Use javascript.

Instead of

header('location: http://www.google.com.au/');

Use,

?>
<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.

But, one precaution is to be made: Javascript redirection will redirect your page even if there is something printed on the page.

Make sure that it does not skip any logic written in PHP.

这篇关于在不使用标头方法的情况下在PHP中重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆