简体   繁体   English

PHP重定向到其他页面,没有空白页面

[英]PHP redirect to onother page without blank page

I have this PHP function for redirect page: 我有此PHP函数用于重定向页面:

function _IS_Redirect_($url) {
    if(!headers_sent()) {
        //If headers not sent yet... then do php redirect
        header('Location: '.$url);
        exit;
    } else {
        //If headers are sent... do javascript redirect... if javascript disabled, do html redirect.
        echo '<script type="text/javascript">';
        echo 'window.location.href="'.$url.'";';
        echo '</script>';
        echo '<noscript>';
        echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
        echo '</noscript>';
        exit;
    }
}

Now, when i submit form if form not have error redirect to another page. 现在,当我提交表单时,如果表单没有错误,则重定向到另一个页面。 now i have 2 problem : 现在我有2个问题:

  • after post submit and when page is redirect i see blank page and then page redirected! 发布后提交后,当页面被重定向时,我看到空白页面,然后页面被重定向! how do redirect page without see blank page? 如何在不看到空白页的情况下重定向页面?
  • i need to show success message in new page. 我需要在新页面中显示成功消息。 how do handle success error to new page ? 如何处理新页面的成功错误?

how do fix this errors? 如何解决此错误?

If you use a javascript or html refresh redirect, you will always see a blank page. 如果您使用javascript或html刷新重定向,则始终会看到空白页。 The best solution is to display a message on the page saying that they will be redirected shortly. 最好的解决方案是在页面上显示一条消息,指出它们将很快被重定向。 In your case showing a success message and that they'll be redirected. 在您的情况下,显示成功消息,并将其重定向。

Ideally you should never need the js or html redirect. 理想情况下,您永远不需要js或html重定向。 If it's an issue, find a way to rearrange the code so that the redirect happens before any other code is run. 如果存在问题,请找到一种方法来重新排列代码,以便在运行任何其他代码之前进行重定向。 And make sure there are no errors causing it. 并确保没有错误导致它。

As for showing a success message on the page the user was redirected to, there's another Stack Overflow Answer that addresses it. 至于在用户重定向到的页面上显示成功消息,还有另一个解决该问题的堆栈溢出答案 The simplest solution in my opinion is to add a GET variable to the end of the url. 我认为最简单的解决方案是在URL的末尾添加一个GET变量。

header('Location: '.$url.'?success=1'); 

On redirected page 在重定向页面上

if ( isset($_GET['success']) && $_GET['success'] == 1 )
{
     echo "Success";
}

An alternative solution is to use a session variable. 另一种解决方案是使用会话变量。 See link to stack overflow question above. 请参阅上面的堆栈溢出问题链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM